I'm trying to finish this last method for the circular array backed queue. I'm stuck on the resize method. Any help or pointers are appreciated.
def resize(self, newsize):
assert(len(self.data) < newsize)
new = Queue(newsize)
for x in range(len(self.data)):
new[x] == self.data[x]
self.data = new
The Error I receive is:
<ipython-input-31-d458e1ceda34> in <module>()
19
20 for i in range(9, 14):
---> 21 q.enqueue(i)
22
23 for i in range(4, 14):
<ipython-input-28-0e6c7038d634> in enqueue(self, val)
9
10 def enqueue(self, val):
---> 11 if self.head == (self.tail + 1) % len(self.data):
12 raise RuntimeError
13 elif self.head == -1 and self.tail == -1:
TypeError: object of type 'Queue' has no len()
My enqueue method looks like this and gives me no errors anywhere else:
def enqueue(self, val):
if self.head == (self.tail + 1) % len(self.data):
raise RuntimeError
elif self.head == -1 and self.tail == -1:
self.head = 0
self.tail = 0
self.data[self.tail] = val
else:
self.tail = (self.tail + 1) % len(self.data)
self.data[self.tail] = val
Queue
class needs __len__
method:
>>> class Queue:
... pass
...
>>> len(Queue())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object of type 'Queue' has no len()
>>> class Queue:
... def __len__(self):
... return 0
...
>>> len(Queue())
0
When a built-in method len
is called it will internally call __len__
of the passed object. If the object doesn't have __len__
defined you'll get the error as the example above shows.