I'm doing some basic Python programming practice exercises and tried to implement a queue (using lists). Unfortunately, I'm getting behavior for my isempty() function that I don't understand. When running the code below, the last two lines give different answers: A yields False, while B yields True. Why doesn't A also yield False?
class Queue:
def __init__(self):
self.items = []
def push(self,item):
self.items.insert(0,item)
def pop(self):
return self.items.pop()
def size(self):
return len(self.items)
def isempty(self):
return self.size == 0
q = Queue()
q.push("a")
q.push("b")
print(q.pop())
print(q.isempty())
print(q.pop())
print(q.isempty()) # shouldn't this (A)...
print(q.size()==0) # ...and this (B) yield the same answer?
Just change your isempty() method to:
def isempty(self):
return self.size() == 0
Your implementation of Queue.isempty() is checking to see if the method size is equal to the integer 0, which will never be true.
class Queue:
def __init__(self):
self.items = []
def push(self,item):
self.items.insert(0,item)
def pop(self):
return self.items.pop()
def size(self):
return len(self.items)
def isempty(self):
return self.size == 0
q = Queue()
print(q.size)
Produces:
<bound method Queue.size of <__main__.Queue object at 0x02F4EA10>>
The easiest solution is to use Christopher Shroba's suggestion to modify your Queue.isempty() implementation to use the list's size method.
Related
I have stack code:
class Stack:
def __init__(self):
self.__data = []
def empty(self):
return len(self.__data) == 0
def size(self):
return len(self.__data)
def push(self, x):
self.__data.append(x)
def pop(self):
return self.__data.pop()
and adds numbers 1, 2:
stack = Stack()
stack.push(1)
stack.push(2)
and I don't know how to print __data list?
so that it shows 1,2 in the list?
[1,2]
As __data is a private attribute of stack object, it cannot be accessed outside the class. Instead define an instance method to print the stack list as shown below.
class Stack:
def print_stack(self):
print(self.__data)
Now if you call print_stack() on an instance. It will print the __data list.
You can use __str__ method to print the values using print() or __repr__ for direct representation.
class Stack:
def __init__(self):
self.data = []
def empty(self):
return len(self.data) == 0
def size(self):
return len(self.data)
def push(self, x):
self.data.append(x)
def pop(self):
return self.data.pop()
def __str__(self):
return str(self.data)
def __repr__(self):
return str(self.data)
>>> stack = Stack()
>>> stack.push(1)
>>> stack.push(2)
>>> print(stack) ## using __str__
# [1, 2]
>>> stack ## using __repr__
# [1, 2]
I have one more question. My code:
class Stack:
def __init__(self):
self.data = []
def empty(self):
return len(self.data) == 0
def size(self):
return len(self.data)
def push(self, x):
self.data.append(x)
def pop(self):
if len(self.data) == 0:
print("underflow")
else:
return self.data.pop()
def __str__(self):
return str(self.data)
stack = Stack()
stack.push(1)
stack.push(2)
print(stack)
I print the list as I wanted:
[1,2]
Now i wonder if i can work on this list? That is, as always on the lists:
In this case:
list = stack
list[0]
1
Can you recommend some simple courses where do Class is explained? I feel confused and my questions seem simple...
Actually my sort algorithm works, but there is a problem.
I have a class namely SortedItem which includes
def __init__(self, point, cost):
self.coordinate = point
self.cost = cost
and I have also priority queue which sorts the this SortedItem by its cost:
class PriorityQueue:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def sortComparatorByCost(self, item):
return item.cost
def enqueue(self, item):
self.items.append(item)
self.items.sort(key=self.sortComparatorByCost, reverse=True)
def dequeue(self):
return self.items.pop()
def returnQueueAsString(self):
queue_str = ""
for eachItem in self.items:
queue_str += str(eachItem) + " "
return queue_str
def isQueueContainsElement(self, element):
for eachElement in self.items:
if eachElement[0] == element:
return True
return False
The problem occurs here:
- I have defined some order to add queue. Let's say I am adding this objects to the queue:
obj1 = SortedItem((1,0), 10))
queue.enqueue(obj1)
obj2 = SortedItem((2,0), 15))
queue.enqueue(obj2)
obj3 = SortedItem((2,1), 15))
queue.enqueue(obj3)
Now I have to get objects from queue in this order (obj1, obj2, obj3).
However python built-in sort function sort these objects like this: (obj1, obj3, obj2) (because obj2 and obj3 has the same cost)
How can i solve this issue. I mean If 2 objects cost is the same, I should get the first added one.
Note that: I have just created a simple example of my problem. If you try this code you may get the objects in this order: obj1, obj2, obj3
Instead of sorting the items in reverse order and removing them from the right,
def enqueue(self, item):
self.items.append(item)
self.items.sort(key=self.sortComparatorByCost, reverse=True)
def dequeue(self):
return self.items.pop()
you could remove them from the left. That would avoid reversing the order of insertion of the items with the same cost.
def enqueue(self, item):
self.items.append(item)
self.items.sort(key=self.sortComparatorByCost)
def dequeue(self):
return self.items.pop(0)
Removing items from the beginning of a list is not efficient, however, so you could better use a deque (replacing pop(0) by popleft()) to fix that. A deque on the other hand, has no in-place sort() method, so would need to replace self.items.sort() by self.items = deque(sorted(self.items)) as well.
so I am trying to create a list of stack objects in Python. I have first created a class Stack that has simple methods that a Stack should have. I have then created another class called Stacks. I am trying to create a list of stacks. If a stack has more than 3 elements, it creates a new stack but I get an error when I try to display the elements. Could someone point out what I might be doing wrong here please?
class Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def size(self):
return len(self.items)
def printStack(self):
for item in reversed(self.items):
print (item)
class Stacks:
def __init__(self):
self.stacks = []
self.noOfStacks = 0
self.itemsOnStack = 0
def dev(self):
self.stacks.append(Stack())
# if len(self.stacks) != 0:
# self.noOfStacks += 1
def push(self, item):
if self.itemsOnStack > 3:
self.dev()
else:
self.itemsOnStack += 1
self.stacks[self.noOfStacks].push(item)
def pop(self, stackNo):
return self.stacks(noOfStacks).pop()
def size(self):
return len(self.stacks)
def printtack(self, index):
print (len(self.stacks(index)))
self.stacks(index).printStack()
stacky = Stacks()
stacky.dev()
stacky.push(3)
stacky.printtack(0)
Indexing lists in Python works by [] not (). Try
def printtack(self, index):
self.stacks[index].printStack()
One thing to note as kshikama said indexing should be done using [] not (),the other problem is using the len() method in the stack class u most override the __len__() method or as u have given use the size() method
class Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def size(self):
return len(self.items)
def printStack(self):
for item in reversed(self.items):
print (item)
class Stacks:
def __init__(self):
self.stacks = []
self.noOfStacks = 0
self.itemsOnStack = 0
def dev(self):
self.stacks.append(Stack())
#if len(self.stacks) != 0:
#self.noOfStacks += 1
def push(self, item):
if self.itemsOnStack > 3:
self.dev()
else:
self.itemsOnStack += 1
self.stacks[self.noOfStacks].push(item)
def pop(self, stackNo):
return self.stacks(noOfStacks).pop()
def size(self):
return len(self.stacks)
def printtack(self, index):
print (self.stacks[index].size())
self.stacks[index].printStack()
stacky = Stacks()
stacky.dev()
stacky.push(3)
stacky.printtack(0)
OUTPUT
1
3
I'm a complete beginner for programming. I created the class "queue",added some elements to it and tried to print each element of the queue as following but I couldn't. Please help me!
Thanks in advance!
edited for formatting
class queue:
def __init__(self):
self.items = []
def isempty(self):
return self.items == []
def enqueue(self,item):
self.items.insert(0,item)
def dequeue(self):
return self.items.pop()
def size(self):
return len(self.items)
def peek(self):
return self.items[len(self.items)-1]
q=queue()
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)
for n in q():
print "This time, it's: "+ str(n)
When you create your own class implementation then you you have to define each and every behaviour of that class, the for loop is only applicable to iterables and to make your object iterable you need to define __iter__ method inside your class which would be called implicitly whenever you try to iterate over your object.
class queue:
def __init__(self):
self.items = []
def isempty(self):
return self.items == []
def enqueue(self,item):
self.items.insert(0,item)
def dequeue(self):
return self.items.pop()
def size(self):
return len(self.items)
def peek(self):
return self.items[len(self.items)-1]
def __iter__(self):
for i in self.items:
yield i
q=queue()
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)
for n in q:
print "This time, it's: "+ str(n)
You are iterating through a non-sequence type:
for n in q():
print "This time, it's: "+ str(n)
This is the correct way:
for n in q.items:
print "This time, it's: "+ str(n)
You either need to define an __iter__ method to make your queue iterable, or you need to modify the loop to use your defined methods.
Here q is an instance of class queue. Also q is not callable.And items is the instance variable.So you have to use
for n in q.items:
print "This time, it's: "+ str(n)
Defining the __getitem__ magic method is possibly the simplest way to make your queue iterable.
class queue:
def __init__(self):
self.items = []
def isempty(self):
return self.items == []
def enqueue(self,item):
self.items.insert(0, item)
def dequeue(self):
return self.items.pop()
def size(self):
return len(self.items)
def peek(self):
return self.items[len(self.items)-1]
def __getitem__(self, i):
return self.items[i]
q=queue()
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)
for n in q:
print "This time, it's: "+ str(n)
Also don't use for n in q():. Thats going to try and call your queue object as a function. As q is not a function, it fails.
As a consequence of implementing __getitem__, you are also able to reference the elements in your queue by index directly on the queue object.
e.g.
print q[0]
prints
3
Well you have to enqueue something in the queue to print it out!
this code after your class
q=queue()
q.enqueue(3)
q.enqueue(4)
q.enqueue(55)
for n in q():
print "This time, it's: "+ str(n)
You never put anything in the queue. So it is empty. Therefore nothing gets printed. Try adding some objects (call the function q.enqueue) and then perhaps they will print.
In my case when import Queue the answer selected above doesn't work.
What really works is that:
for i in q.queue:
print i
Ok so im trying to input a word in a stack and I want to print all of them after I input a string. So I can only print them one at a time. I tried using a for loop outside but Stacks are apparently not iterable. So I iterating it inside the stack. It still is not working.
class Stack:
def __init__(self):
self.items = []
def push(self,items):
self.items.insert(0,items)
def pop(self):
for x in self.items:
print( self.items.pop(0))
def show(self):
print (self.items)
s = Stack()
s.show()
placed = input("enter")
item = s.pop()
print(item, "is on top", s)
Give your Stack class a __len__ method, this will make testing if the stack is empty easier:
class Stack:
def __init__(self):
self.items = []
def push(self,item):
self.items.append(item)
def pop(self):
return self.items.pop()
def show(self):
print (self.items)
def __len__(self):
return len(self.items)
stack = Stack()
stack.push('World!')
stack.push('Hello')
while stack: # tests the length through __len__
print(stack.pop())
Note that I simply .append() to the end of the .items list, then later on .pop() (no arguments) again, removing from the end of the list.
To make your class an iterable type, you'd need to add at least an __iter__ method, optionally together with a .__next__() method:
class Stack:
# rest elided
def __iter__(self):
return self
def next(self):
try:
return self.items.pop()
except IndexError: # empty
raise StopIteration # signal iterator is done