Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nourhan - Spruce - C16 #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions stacks_queues/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,58 @@ def enqueue(self, element):
In the store are occupied
returns None
"""
pass
if self.size == INITIAL_QUEUE_SIZE:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest using buffer_size over INITIAL_QUEUE_SIZE here. Imagine you alter your implementation so that the buffer_size could expand in certain cases. Then you would have to manually change all of your references to INITIAL_QUEUE_SIZE to maintain your code.

Suggested change
if self.size == INITIAL_QUEUE_SIZE:
if self.size == self.buffer_size:

raise QueueFullException()
index = self.rear % self.buffer_size
self.store[index] = element
self.rear -= 1
self.size += 1
Comment on lines +28 to +31

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting to decrease the rear pointer instead of increasing it! 😎


def dequeue(self):
""" Removes and returns an element from the Queue
Raises a QueueEmptyException if
The Queue is empty.
"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass
if self.size == 0:
raise QueueEmptyException()
index = self.front % self.buffer_size
element = self.store[index]
self.front -= 1
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.
"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass
if self.size == 0:
return None
index = self.front % self.buffer_size
return self.store[index]


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
return self.size == 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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
result = []

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for index in range(self.front, self.front - self.size, -1):
result.append(self.store[index % self.buffer_size])
return str(result)


19 changes: 14 additions & 5 deletions stacks_queues/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,36 @@ def push(self, element):
""" Adds an element to the top of the Stack.
Returns None
"""
pass
self.store.add_last(element)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


def pop(self):
""" Removes an element from the top
Of the Stack
Raises a StackEmptyException if
The Stack is empty.
returns None
returns the removed element
"""
pass
if self.empty():
raise StackEmptyException()

return self.store.remove_last()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.



def empty(self):
""" Returns True if the Stack is empty
And False otherwise
"""
pass
return self.store.empty()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


def __str__(self):
""" Returns the Stack in String form like:
[3, 4, 7]
Starting with the top of the Stack and
ending with the bottom of the Stack.
"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ This does work, but you could also take advantage of the str method in the LinkedList class

pass
values = []

current = self.store.get_last()
while current:
values.append(str(current.value))
current = current.prev