in the Node class for some reason the val function works wierd
>>> a= Node(1) >>> a.val <bound method Node.val of 1> >>> a.value 1
what does it do?
No, it cannot be O(1), if it generates a representation of the whole deque (to generate an object of size O(x) you need at least O(x) time).
thats what i thought, but the instructions sayd "everything"..
is it allowed to edit the Node class or should we use it as it is?
Is it possible you intended to put a doubly-linked node class instead of the singly-linked one that's in the skeleton file?
it's very simple to change it to be a node of a double-linked list,
but the question is - do we allowed to edit code from a skeleton file?
You are allowed to add fields to class Node.
by 'add fields' you mean that we can create new methods in class node as well as changing the 'built-in' methods in the skeleton file?
Are there any limitations on init functions in Deque or can we implement however we want?
There is about init in the instructions.
and also, next function in class Node means we must use it as an iterator (since there is a problem with the function - there another varieable in the class of same name)? can we change the name of the function?
can we change the name of next? or the name of the inner field next?
otherwise it creates a problem:
>>> a=Node(5) >>> a.addNext(6) >>> a 5 >>> a.next 6 >>> next(a) Traceback (most recent call last): File "<pyshell#13>", line 1, in <module> next(a) TypeError: Node object is not an iterator >>> a.next() Traceback (most recent call last): File "<pyshell#14>", line 1, in <module> a.next() TypeError: 'int' object is not callable >>> a.next(a) Traceback (most recent call last): File "<pyshell#15>", line 1, in <module> a.next(a) TypeError: 'int' object is not callable >>>
i can't think of another way of calling a function…
You tried calling next() on an object of class Node. But next() can only be called for iterators, i.e. objects that have a method next. Node does not have it. [This is just like we saw in the recitation, when trying to call next() on class B].
Instead, simply call a.next(), just like you would call any method of class Node.
i don't understand.
like you said earlier it's next and not next so it should work like any other regular function shouldn't it? it's not the next for iterators in class Node().
and besides, the second time here i called
>>> a.next() Traceback (most recent call last): File "<pyshell#14>", line 1, in <module> a.next() TypeError: 'int' object is not callable
and it doesn't work since he turns to the integer and not the function.
You are right - the code of class Node contains both a field named next and a method with the same name.
Therefore, when trying to call a.next(), python thinks you're referring to the field next, and use it as a function.
This is indeed problematic, and calling a method and a field the same name is wrong.
So simply use the field and not the method whenever you want to reach the next element.
This comment changes the answer completely - I think it is necessary to alert everyone about this. I worked hours to solve it until I saw this comment and had to rewrite everything. I think it is unfair that you assume everyone reads every single post in the forum and gets to the relevant messages when needed - how could I have known this significant change in requirements? Why not send an email about something like this?
There was no "change in requirements". Understanding that you need to change class Node is part of what we intended for you to realize in this question. We also expect you to understand that chainging internal representation of a class is OK, as long as interface is unchanged.
Reading the forum posts is part of your learning process, and we definitely expect you to do so.
It depends on how your head and tail fields are called (you'd better call them front and rear, as head() is actually a function inside the Deque class, and not a field).
there is same problem with the
function in the Node class.
If "init" at 'Deque' Class will contain another variable besides "head" and "tail", will it cause a loss of points?
No, as Amir wrote earlier, we are allowed to add our own fields to the Node class.
Regarding the remove head and tail functions - when given an empty list - should we assert or just return the same empty list unchanged?
why should the methods head() and tail() return objects of type "Node" and not their values?
isn't it a mistake of implementation?
if I'm a user that called Dequeue and passed it the integers 1 and 2, when I call head() and tail() I expect to recieve 1 and 2, and not the nodes that hold 1 and 2 as their values.