Understanding Linked Lists in Java - linked-list

Using the following list above, I'm attempting to determine the value of the following expressions below. This topic is relatively new in my course. (I'll highlight my answers in bold for you to see what I have tried.)
temp : [7]]
temp.value : 7
temp.next.value : 3
head.next : [5]]
temp.next.next : 2
temp.value.next : ?
head.next.next.value : 7
temp.next.next.next.value : null

I'll assume that the notation for a node is to wrap its value in square brackets -- which you have done for some answers.
Here is your list with corrections and remarks:
Expression
Your answer
Review
temp
[7]]
temp.value
7
temp.next.value
3
head.next
[5]]
temp.next.next
2
[2]]
temp.value.next
?
error
head.next.next.value
7
temp.next.next.next.value
null
error
So most of your answers are correct, but temp.next.next is a node, not an integer, so it is [2]] instead of 2.
As temp.value is an integer, temp.value.next is an invalid property reference, and will raise an exception.
As temp.next.next.next is null (marked with a cross in the picture), temp.next.next.next.value is also an invalid property reference, and will raise an exception.

Related

How do I create a DCG rule inverse to another in Prolog?

I am writing a Commodore BASIC interpreter in Prolog, and I am writing some DCGs to parse it. I have verified the DCGs below to work except for the variable one. My goal is this: for anything which isn't a boolean, integer, float, or a string, it's a variable. However, anything that I give it via phrase just results in no.
bool --> [true].
bool --> [false].
integer --> [1]. % how to match nums?
float --> [0.1].
string --> [Str], {atom_chars(Str, ['"' | Chars]), last(Chars, '"')}.
literal --> bool; integer; float; string.
variable --> \+ literal.
I ran a stack trace like this (with gprolog)
main :- trace, phrase(variable, [bar]).
Looking at this, I cannot figure out why variable fails, given that it fails for each case in literal. I'm guessing that the error is pretty simple, but I'm still stumped, so does anyone who's good at Prolog have an idea of what I'm doing wrong?
| ?- main.
The debugger will first creep -- showing everything (trace)
1 1 Call: phrase(variable,[bar]) ?
2 2 Call: variable([bar],_321) ?
3 3 Call: \+literal([bar],_348) ?
4 4 Call: literal([bar],_348) ?
5 5 Call: bool([bar],_348) ?
5 5 Fail: bool([bar],_348) ?
5 5 Call: integer([bar],_348) ?
5 5 Fail: integer([bar],_348) ?
5 5 Call: float([bar],_348) ?
5 5 Fail: float([bar],_348) ?
5 5 Call: string([bar],_348) ?
6 6 Call: atom_chars(bar,['"'|_418]) ?
6 6 Fail: atom_chars(bar,['"'|_418]) ?
5 5 Fail: string([bar],_348) ?
4 4 Fail: literal([bar],_348) ?
3 3 Exit: \+literal([bar],_348) ?
2 2 Exit: variable([bar],[bar]) ?
1 1 Fail: phrase(variable,[bar]) ?
(2 ms) no
{trace}
To expand a bit on the other answer, the key problem is that a DCG rule like \+ literal does not consume items from the input. It only checks that the next item, if any, is not a literal.
To actually consume an item, you need to use a list goal, similarly to how you use a goal [1] to consume a 1 element. So:
variable -->
\+ literal, % if there is a next element, it's not a literal
[_Variable]. % consume this next element, which is a variable
For example:
?- phrase(variable, [bar]).
true.
?- phrase((integer, variable, float), [I, bar, F]).
I = 1,
F = 0.1.
Having that singleton variable _Variable is a bit strange -- when you parse like this, you lose the name of the variable. When your parser is expanded a bit, you will want to use arguments to your DCG rules to communicate information out of the rules:
variable(Variable) -->
\+ literal,
[Variable].
For example:
?- phrase((integer, variable(Var1), float, variable(Var2)), [I, bar, F, foo]).
Var1 = bar,
Var2 = foo,
I = 1,
F = 0.1.
You can detect a string of integers like this (I've added an argument to collect the digits):
integer([H|T]) --> digit(H), integer(T).
integer([]) --> [].
digit(0) --> "0".
digit(1) --> "1".
...
digit(9) --> "9".
As for variable -- it needs to consume text, so you'd want something similar to integer above, but change digit(H) to something that recognizes a character that's part of a "variable".
If you want further clues (although sometimes using slightly advanced tricks): https://www.swi-prolog.org/pldoc/man?section=basics and the code is here: https://github.com/SWI-Prolog/swipl-devel/blob/master/library/dcg/basics.pl

No '...' candidates produce 'Range<String.Index>'

While converting an old iOS app to Sift 3.0 I hit the following issue:
The code is:
cutRange = numberString.index(numberString.startIndex, offsetBy:2)...numberString.index(numberString.startIndex, offsetBy:5)
The error message I get is:
No '...' candidates produce the expected contextual result type 'Range<String.Index>' (aka 'Range<String.CharacterView.Index>')
I have seen a few post related to the subject, but was not very satisfied.
So what is the simplest way to solve this problem?
In Swift 3, two range operators generate different results:
closed range operator ... -> ClosedRange (by default)
(half open) range operator ..< -> Range (by default)
So, assuming your cutRange is declared as Range<String.Index>, you need to use half open range operator ..<:
cutRange = numberString.index(numberString.startIndex, offsetBy:2)..<numberString.index(numberString.startIndex, offsetBy:6)
(Please do not miss the last offset is changed to 6.)

Lua Torch7 array index notation

I have an array named xTain of size nDatax1
I initialize it as
xTrain = torch.linspace(-1,1,nData)
To access the array, the author uses xTrain[{{i}}]
can you please explain this notation? Why not simply xTrain[i] ?
Please refer the author's code here on Pg No 21- https://www.cs.ox.ac.uk/people/nando.defreitas/machinelearning/lecture4.pdf
As an additional note-
xTrain=torch.linespace(-1,1,10)
when I do
th> print(xTrain[1])
-1
th> print(xTrain[{1}])
-1
th> print(xTrain[{{1}}])
-1
[torch.DoubleTensor of size 1]
Why does it also print [torch.DoubleTensor of size 1] in 3rd case. My guess is in first 2 case its returning a scalar value at that location and in 3rd case a DoubleTensor
Good place to start is the Lua manual, it's syntax and explessions. You can see, what is meaning of {...} in Lua:
{...} -- creates a list with all vararg parameters
So in short your {1} creates a list with single value 1. Repeating it once more you got a list containing list containing single number 1.
If the xTrain would be simple table, it would probably fail, because it is hard to index using lists, but Lua supports metatables so the actual value is not used for indexing the table, but passed to some function which takes care of the lists.
Also reading further about the Tensor class, which is returned from the torch.linespace() function is a good place to see. The indexing using "array access" is explained in the section [Tensor] [{ dim1,dim2,... }] or [{ {dim1s,dim1e}, {dim2s,dim2e} }]

Issue with GameNetwork and Corona SDK

From the picture above in matchDataListener function in line 225, the question is what is the meaning of matchDataEvent.data.data. I have already explored to the reference in http://docs.coronalabs.com/api/type/GameNetwork/match.html but there is no any explanation.
I also don't understand the meaning of
for i = 1, #matchDataEvent.data.participants, 1
in line 244
Could anyone explain them to me?
matchDataEvent.data.data refers to the string inside matchDataEvent.data table/array. I've no idea about what it generally contains though. The line #225 in particular; checks if this string exists and in not empty.
In #matchDataEvent.data.participants, the # is the __len method which returns the length of the table/array matchDataEvent.data.participants. Total number of participants are returned (a number). This number is used as the max value in the for loop.

MVC jqGrid Small Integer Error

This article is really great and awesome. This is from the topic "ASP.NET MVC 2.0 Implementation of searching in jqgrid" - ASP.NET MVC 2.0 Implementation of searching in jqgrid.
But right now I was facing searching problem when I added a field with small integer data type. The field added with data type of small integer will serve as Status.
Lets say the value one(1) is for Active and value two(2) is for Inactive.
When I type 1 or 2 from the text box it was throwing an error of
System.Data.Entity: The argument types 'Edm.Int16' and 'Edm.String' are incompatible for this operation. Near equals expression, line 6, column 12.
Thank you in advance.
I am glad that my old answer was helpful for you. I suppose you have problem near the line
// TODO: Extend to other data types
In the code which I included in the answer I shown that propertyInfo.PropertyType.FullName hold the information about the datatype of the entity's property. In the code I used only two types: string and 32-bit integer. In case of 32-bit integers I made the corresponding data parsing with respect of Int32.Parse:
String.Compare (propertyInfo.PropertyType.FullName,
"System.Int32", StringComparison.Ordinal) == 0 ?
new ObjectParameter ("p" + iParam, Int32.Parse(rule.data)
You should replace " ? : " operator with case where you test propertyInfo.PropertyType.FullName for more data types. For example in case of smallint SQL type you should use System.Int16, in case of tinyint it should be System.Byte, The sbyte SQL datatype corresponds to System.SByte and so on. If you would use as the second parameter of ObjectParameter correct datatype all should work correctly

Resources