class CircularQueue:
def __init__(self, capacity):
if type(capacity) != int or capacity < 0:
raise Expection("capacity")
self.queue = []
self.count = 0
self.max = capacity
def enqueue(self, item):
if self.count == self.max:
raise Expection("queue is full")
self.queue.append(item)
self.count += 1
def dequeue(self):
if self.count == 0:
raise("empty!")
self.count -= 1
return self.queue.pop(0)
def peek(self):
if self.count == 0:
raise("empty!")
return self.queue[0]
def is_empty(self):
return 0 == self.count
def is_full(self):
return self.max == self.count
def size(self):
return self.count
def capacity(self):
return self.max
def clear(self):
self.queue = []
self.count = 0
def __str__(self):
return " ".join([str(v) for v in self.queue])
# use this function to test your Circular Queue implementation
def test():
string_queue = CircularQueue(3)
is_pass = (string_queue.size() == 0)
assert is_pass == True, "fail the test"
is_pass = (string_queue.capacity() == 3)
assert is_pass == True, "fail the test"
is_pass = (string_queue.is_empty())
assert is_pass == True, "fail the test"
string_queue.enqueue("Hello")
string_queue.enqueue("World")
is_pass = (str(string_queue) == "Hello World")
assert is_pass == True, "fail the test"
is_pass = (string_queue.size() == 2)
assert is_pass == True, "fail the test"
is_pass = (string_queue.peek() == "Hello")
assert is_pass == True, "fail the test"
is_pass = (string_queue.capacity() == 3)
assert is_pass == True, "fail the test"
is_pass = (string_queue.peek() == "Hello" and string_queue.size() == 2)
assert is_pass == True, "fail the test"
string_queue.enqueue("python")
is_pass = (string_queue.is_full())
assert is_pass == True, "fail the test"
try:
string_queue.enqueue("rules")
except Exception as e:
is_pass = True
else:
is_pass = False
assert is_pass == True, "fail the test"
string_queue.dequeue()
is_pass = (string_queue.peek() == "World")
assert is_pass == True, "fail the test"
string_queue.dequeue()
is_pass = (string_queue.peek() == "python")
assert is_pass == True, "fail the test"
string_queue.enqueue("apple")
string_queue.enqueue("google")
string_queue.dequeue()
is_pass = (string_queue.peek() == "apple")
assert is_pass == True, "fail the test"
string_queue.clear()
is_pass = (string_queue.capacity() == 3)
assert is_pass == True, "fail the test"
is_pass = (string_queue.size() == 0)
assert is_pass == True, "fail the test"
try:
string_queue.dequeue()
except Exception as e:
is_pass = True
else:
is_pass = False
assert is_pass == True, "fail the test"
try:
string_queue.peek()
except Exception as e:
is_pass = True
else:
is_pass = False
assert is_pass == True, "fail the test"
int_queue2 = CircularQueue(10)
for i in range(0, 10):
int_queue2.enqueue(i)
int_queue2.dequeue()
int_queue2.dequeue()
int_queue2.dequeue()
int_queue2.dequeue()
is_pass = (str(int_queue2) == "4 5 6 7 8 9")
assert is_pass == True, "fail the test"
for i in range(11, 13):
int_queue2.enqueue(i)
is_pass = (str(int_queue2) == "4 5 6 7 8 9 11 12")
assert is_pass == True, "fail the test"
for i in range(21, 23):
int_queue2.enqueue(i)
is_pass = (str(int_queue2) == "4 5 6 7 8 9 11 12 21 22")
assert is_pass == True, "fail the test"
int_queue = CircularQueue(1000)
is_pass = (int_queue.is_empty())
assert is_pass == True, "fail the test"
is_pass = (int_queue.is_full())
assert is_pass == False, "fail the test"
for i in range(0, 1000):
int_queue.enqueue(i)
correctOrder = True
is_pass = (int_queue.is_empty())
assert is_pass == False, "fail the test"
is_pass = (int_queue.is_full())
assert is_pass == True, "fail the test"
for i in range(0, 200):
if int_queue.dequeue() != i:
correctOrder = False
is_pass = correctOrder
assert is_pass == True, "fail the test"
is_pass = (int_queue.size() == 800)
assert is_pass == True, "fail the test"
is_pass = (int_queue.capacity() == 1000)
assert is_pass == True, "fail the test"
is_pass = (int_queue.peek() == 200)
assert is_pass == True, "fail the test"
for i in range(0, 200):
int_queue.enqueue(i)
is_pass = (int_queue.is_empty())
assert is_pass == False, "fail the test"
is_pass = (int_queue.is_full())
assert is_pass == True, "fail the test"
is_pass = (int_queue.peek() == 200)
assert is_pass == True, "fail the test"
if is_pass == True:
print ("=========== Congratulations! Your have finished exercise 1! ============")
if __name__ == '__main__':
test()
I have completed this program and it works fine but need create a private attribute (internal data object) to record how many items are currently stored in the queue. Thus when the start and end pointer both point to the same element I can check this counter to know whether the queue is empty or full.
Can I get some help?

New Topic/Question
Reply



MultiQuote





|