From c59f3fad15305886df28cc15b63ac981667d4ca8 Mon Sep 17 00:00:00 2001 From: Hena Date: Sat, 18 Dec 2021 16:33:59 -0600 Subject: [PATCH 1/2] Passes all tests --- stacks_queues/queue.py | 45 +++++++++++++++++++++++++++++++++++------- stacks_queues/stack.py | 11 +++++++---- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/stacks_queues/queue.py b/stacks_queues/queue.py index cc73ad9..95d193d 100644 --- a/stacks_queues/queue.py +++ b/stacks_queues/queue.py @@ -15,7 +15,7 @@ def __init__(self): self.front = -1 self.rear = -1 self.size = 0 - + def enqueue(self, element): """ Adds an element to the Queue @@ -23,7 +23,21 @@ def enqueue(self, element): In the store are occupied returns None """ - pass + if self.size == self.buffer_size: + raise QueueFullException("The queue is full.") + + elif self.front == -1: + self.front = 0 + self.rear = 0 + self.store[self.rear] = element + self.size += 1 + return self.store + + else: + self.rear = (self.rear + 1) % self.buffer_size + self.store[self.rear] = element + self.size +=1 + return self.store def dequeue(self): """ Removes an element from the Queue @@ -31,27 +45,36 @@ def dequeue(self): The Queue is empty. returns None """ - pass + if self.front == -1: + raise QueueEmptyException("The queue is empty.") + + element = self.store[self.front] + self.front = (self.front + 1) % self.buffer_size + self.size -= 1 + return element 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.empty(): + return None + return self.store[self.front] def size(self): """ Returns the number of elements in The Queue """ - pass + return self.size() def empty(self): """ Returns True if the Queue is empty And False otherwise. """ - pass + if self.size == 0: + return True def __str__(self): """ Returns the Queue in String form like: @@ -59,4 +82,12 @@ def __str__(self): Starting with the front of the Queue and ending with the rear of the Queue. """ - pass + queue_list = [] + + if self.empty(): + return str(queue_list) + + for element in range(self.front, self.front + self.size): + element = element % self.buffer_size + queue_list.append(self.store[element]) + return str(queue_list) diff --git a/stacks_queues/stack.py b/stacks_queues/stack.py index 94fb2a6..7e14604 100644 --- a/stacks_queues/stack.py +++ b/stacks_queues/stack.py @@ -12,7 +12,7 @@ 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 +21,16 @@ def pop(self): The Stack is empty. returns None """ - pass + if not self.store: + raise StackEmptyException("The stack is empty.") + self.store.remove_first() + def empty(self): """ Returns True if the Stack is empty And False otherwise """ - pass + return self.store.empty() def __str__(self): """ Returns the Stack in String form like: @@ -35,4 +38,4 @@ def __str__(self): Starting with the top of the Stack and ending with the bottom of the Stack. """ - pass + return str(self.store) From 90b205d125f28c509c8117d4bc473024566c05fe Mon Sep 17 00:00:00 2001 From: Hena Date: Sat, 18 Dec 2021 16:38:39 -0600 Subject: [PATCH 2/2] Passes all tests for real this time --- stacks_queues/stack.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/stacks_queues/stack.py b/stacks_queues/stack.py index 7e14604..1017817 100644 --- a/stacks_queues/stack.py +++ b/stacks_queues/stack.py @@ -21,10 +21,7 @@ def pop(self): The Stack is empty. returns None """ - if not self.store: - raise StackEmptyException("The stack is empty.") - self.store.remove_first() - + return self.store.remove_first() def empty(self): """ Returns True if the Stack is empty