From 823d82dcd5406ac622d182f4c9c99653bac4a8d4 Mon Sep 17 00:00:00 2001 From: ggrossvi Date: Sun, 21 Nov 2021 13:07:53 -0800 Subject: [PATCH 1/4] implemented pushpop and empty --- stacks_queues/linked_list.py | 2 ++ stacks_queues/queue.py | 3 +-- stacks_queues/stack.py | 17 ++++++++++++++--- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/stacks_queues/linked_list.py b/stacks_queues/linked_list.py index 3cb0aa4..934ba1d 100644 --- a/stacks_queues/linked_list.py +++ b/stacks_queues/linked_list.py @@ -1,4 +1,6 @@ + + class EmptyListError(Exception): pass diff --git a/stacks_queues/queue.py b/stacks_queues/queue.py index d66dab2..5cdf137 100644 --- a/stacks_queues/queue.py +++ b/stacks_queues/queue.py @@ -1,4 +1,3 @@ - INITIAL_QUEUE_SIZE = 20 class QueueFullException(Exception): @@ -58,4 +57,4 @@ def __str__(self): Starting with the front of the Queue and ending with the rear of the Queue. """ - pass + pass \ No newline at end of file diff --git a/stacks_queues/stack.py b/stacks_queues/stack.py index 94fb2a6..2f277e8 100644 --- a/stacks_queues/stack.py +++ b/stacks_queues/stack.py @@ -12,7 +12,8 @@ def push(self, element): """ Adds an element to the top of the Stack. Returns None """ - pass + self.store.add_first(element) + def pop(self): """ Removes an element from the top @@ -21,13 +22,23 @@ def pop(self): The Stack is empty. returns None """ - pass + try: + return self.store.remove_first() + + except LinkedList.EmptyListError(): + raise StackEmptyException("The Stack is empty") + + + + def empty(self): """ Returns True if the Stack is empty And False otherwise """ - pass + if self.store.empty(): + return True + return False def __str__(self): """ Returns the Stack in String form like: From a1834d462455176d94b55c8c52d22e8cf5ea084f Mon Sep 17 00:00:00 2001 From: ggrossvi Date: Thu, 25 Nov 2021 11:07:50 -0800 Subject: [PATCH 2/4] fixed test with large queue --- stacks_queues/linked_list.py | 3 - stacks_queues/queue.py | 127 ++++++++++++++++++++++++++++++++--- stacks_queues/stack.py | 22 +++++- 3 files changed, 137 insertions(+), 15 deletions(-) diff --git a/stacks_queues/linked_list.py b/stacks_queues/linked_list.py index 934ba1d..4a47373 100644 --- a/stacks_queues/linked_list.py +++ b/stacks_queues/linked_list.py @@ -1,6 +1,3 @@ - - - class EmptyListError(Exception): pass diff --git a/stacks_queues/queue.py b/stacks_queues/queue.py index 5cdf137..0f97048 100644 --- a/stacks_queues/queue.py +++ b/stacks_queues/queue.py @@ -1,10 +1,39 @@ -INITIAL_QUEUE_SIZE = 20 +from typing import ItemsView + +INITIAL_QUEUE_SIZE = 20 +""" +https://towardsdatascience.com/circular-queue-or-ring-buffer-92c7b0193326 +https://www.geeksforgeeks.org/circular-queue-set-1-introduction-array-implementation/ +""" class QueueFullException(Exception): - pass + def __init__(self, *args): + if args: + self.message = args[0] + else: + self.message = None + + def __str__(self): + print('calling str') + if self.message: + return 'QueueFullException, {0}'.format(self.message) + else: + return 'QueueFullException has been raised' + class QueueEmptyException(Exception): - pass + def __init__(self, *args): + if args: + self.message = args[0] + else: + self.message = None + + def __str__(self): + print('calling str') + if self.message: + return 'QueueEmptyException, {0}'.format(self.message) + else: + return 'QueueEmptyException has been raised' class Queue: @@ -22,39 +51,115 @@ def enqueue(self, element): In the store are occupied returns None """ - pass + if ((self.rear +1) % INITIAL_QUEUE_SIZE ==self.front): + raise QueueFullException('This will break it') + + elif (self.front == -1): + self.front = 0 + self.rear = 0 + self.store[self.rear] = element + self.size = self.size + 1 + else: + + # next position of rear + self.rear = (self.rear + 1) % INITIAL_QUEUE_SIZE + self.store[self.rear] = element + self.size = self.size + 1 + + # if self.size == self.buffer_size: + # raise QueueFullException('This will break it') + + # else: + # self.rear = (self.rear + 1) % INITIAL_QUEUE_SIZE + # self.store[self.rear] = element + # self.size = self.size + 1 + # if self.front == -1: + # self.front = 0 + def dequeue(self): """ Removes and returns an element from the Queue Raises a QueueEmptyException if - The Queue is empty. + The Queue is empty. In the circular Queue it remove the item at the front """ - pass + + if self.empty(): + raise QueueEmptyException("The Stack is empty") + + # condition for only one element + elif (self.front == self.rear): + tmp=self.store[self.front] + self.front = -1 + self.rear = -1 + self.size -=1 + return tmp + + else: + tmp = self.store[self.front] + self.front = (self.front + 1) % INITIAL_QUEUE_SIZE + self.size -=1 + return tmp def front(self): """ Returns an element from the front of the Queue and None if the Queue is empty. Does not remove anything. """ - pass + if self.size == 0: + return None + return self.store[self.front] + + def size(self): """ Returns the number of elements in The Queue """ - pass - + return self.size + # self if a reference to the current class when in a class method you need to append self to it. def empty(self): """ Returns True if the Queue is empty And False otherwise. """ - pass + if self.size == 0: + return True + return False + def __str__(self): """ Returns the Queue in String form like: [3, 4, 7] Starting with the front of the Queue and ending with the rear of the Queue. + """ - pass \ No newline at end of file + tmp = [] + if(self.front == -1): + print ("Queue is Empty") + + elif (self.rear >= self.front): + for i in range(self.front, self.rear + 1): + tmp.append(self.store[i]) + + + else: + for i in range(self.front, INITIAL_QUEUE_SIZE): + tmp.append(self.store[i]) + for i in range(0, self.rear + 1): + tmp.append(self.store[i]) + + + if ((self.rear + 1) % INITIAL_QUEUE_SIZE == self.front): + print("Queue is Full") + + return str(tmp) + + # temp = [] + # for i in range(self.front,self.rear+1): + # if self.store[i]!= None: + # temp.append(self.store[i]) + # return str(temp) + + + \ No newline at end of file diff --git a/stacks_queues/stack.py b/stacks_queues/stack.py index 2f277e8..1940bb9 100644 --- a/stacks_queues/stack.py +++ b/stacks_queues/stack.py @@ -1,7 +1,27 @@ from stacks_queues.linked_list import LinkedList class StackEmptyException(Exception): - pass + ''' + https://towardsdatascience.com/how-to-define-custom-exception-classes-in-python-bfa346629bca + ''' + def __init__(self, *args): + if args: + self.message = args[0] + else: + self.message = None + + def __str__(self): + print('calling str') + if self.message: + return 'StackEmptyException, {0}'.format(self.message) + else: + return 'StackEmptyException has been raised' + ''' + raise StackEmptyException # MyCustom Error is being raised without any arguments, so None will be sent to the message attribute in the object. The str method will be called and will print the message 'MyCustomEror message has been raised' + raise StackEmptyException('We have a problem') + ''' + + class Stack: From dadeb44bde8b3d805b1d6f55f86ccfba2ab917e6 Mon Sep 17 00:00:00 2001 From: ggrossvi Date: Sun, 12 Dec 2021 14:28:40 -0800 Subject: [PATCH 3/4] cleaned up - completed --- stacks_queues/queue.py | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/stacks_queues/queue.py b/stacks_queues/queue.py index 0f97048..1490624 100644 --- a/stacks_queues/queue.py +++ b/stacks_queues/queue.py @@ -66,17 +66,6 @@ def enqueue(self, element): self.store[self.rear] = element self.size = self.size + 1 - # if self.size == self.buffer_size: - # raise QueueFullException('This will break it') - - # else: - # self.rear = (self.rear + 1) % INITIAL_QUEUE_SIZE - # self.store[self.rear] = element - # self.size = self.size + 1 - # if self.front == -1: - # self.front = 0 - - def dequeue(self): """ Removes and returns an element from the Queue Raises a QueueEmptyException if @@ -109,9 +98,6 @@ def front(self): return None return self.store[self.front] - - - def size(self): """ Returns the number of elements in The Queue @@ -155,11 +141,7 @@ def __str__(self): return str(tmp) - # temp = [] - # for i in range(self.front,self.rear+1): - # if self.store[i]!= None: - # temp.append(self.store[i]) - # return str(temp) + \ No newline at end of file From d8d5e41672aa5557fe4cd4b2d0a23c82dbb0bde0 Mon Sep 17 00:00:00 2001 From: ggrossvi Date: Tue, 14 Dec 2021 19:54:54 -0800 Subject: [PATCH 4/4] cleaned up file --- stacks_queues/queue.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/stacks_queues/queue.py b/stacks_queues/queue.py index 1490624..8fb33a9 100644 --- a/stacks_queues/queue.py +++ b/stacks_queues/queue.py @@ -2,10 +2,7 @@ INITIAL_QUEUE_SIZE = 20 -""" -https://towardsdatascience.com/circular-queue-or-ring-buffer-92c7b0193326 -https://www.geeksforgeeks.org/circular-queue-set-1-introduction-array-implementation/ -""" + class QueueFullException(Exception): def __init__(self, *args): if args: