-
Notifications
You must be signed in to change notification settings - Fork 64
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
Lety Palomo -Pine C-16 queue and stack project #57
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -23,39 +23,88 @@ def enqueue(self, element): | |||||||||||
In the store are occupied | ||||||||||||
returns None | ||||||||||||
""" | ||||||||||||
pass | ||||||||||||
if self.size == self.buffer_size: | ||||||||||||
raise QueueFullException ("Queue is full") | ||||||||||||
|
||||||||||||
if self.size == 0: | ||||||||||||
self.front = 0 | ||||||||||||
self.rear = 0 | ||||||||||||
|
||||||||||||
#per the test, don't duplicate the item if it was just enqueued | ||||||||||||
if self.store[(self.rear - 1 ) % self.buffer_size] != element: | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 I'm curious which test caused you to add this clause here. It shouldn't be necessary There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I went through lots of iterations to debug the tests. This is leftovers lol! |
||||||||||||
self.store[self.rear] = element | ||||||||||||
self.rear = (self.rear + 1 ) % self.buffer_size | ||||||||||||
self.size = self.size + 1 | ||||||||||||
|
||||||||||||
def dequeue(self): | ||||||||||||
""" Removes and returns an element from the Queue | ||||||||||||
Raises a QueueEmptyException if | ||||||||||||
The Queue is empty. | ||||||||||||
""" | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ |
||||||||||||
pass | ||||||||||||
#Check if empty, if so raise an exception | ||||||||||||
if self.size == 0: | ||||||||||||
raise QueueEmptyException("Queue is empty") | ||||||||||||
|
||||||||||||
retVal = self.store[self.front] | ||||||||||||
#Find and store the front element | ||||||||||||
self.store[self.front] = None | ||||||||||||
#Move front to the next index | ||||||||||||
self.front = (self.front + 1 ) % self.buffer_size | ||||||||||||
# self.rear = (self.rear + 1 ) % self.buffer_size | ||||||||||||
self.size = self.size - 1 | ||||||||||||
return retVal | ||||||||||||
|
||||||||||||
|
||||||||||||
|
||||||||||||
def front(self): | ||||||||||||
""" Returns an element from the front | ||||||||||||
of the Queue and None if the Queue | ||||||||||||
is empty. Does not remove anything. | ||||||||||||
""" | ||||||||||||
pass | ||||||||||||
|
||||||||||||
return self.store[self.front] | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ |
||||||||||||
|
||||||||||||
|
||||||||||||
def size(self): | ||||||||||||
""" Returns the number of elements in | ||||||||||||
The Queue | ||||||||||||
""" | ||||||||||||
pass | ||||||||||||
return self.size | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ |
||||||||||||
|
||||||||||||
def empty(self): | ||||||||||||
""" Returns True if the Queue is empty | ||||||||||||
And False otherwise. | ||||||||||||
""" | ||||||||||||
pass | ||||||||||||
if self.size == 0: | ||||||||||||
return True | ||||||||||||
else: | ||||||||||||
return False | ||||||||||||
Comment on lines
+78
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small style suggestion
Suggested change
|
||||||||||||
|
||||||||||||
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. | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ |
||||||||||||
""" | ||||||||||||
pass | ||||||||||||
returnArr = [] | ||||||||||||
|
||||||||||||
#if the queue rolls around past the buffer | ||||||||||||
#we will break it into two loops | ||||||||||||
if self.front + self.size > self.buffer_size: | ||||||||||||
for i in range (self.front, (self.size - (self.buffer_size % self.size))): | ||||||||||||
# keeping this hear in case we dont want to do the modulo math above and | ||||||||||||
# really want to iterate through to the self.buffer_size as the end of the range | ||||||||||||
# if self.store[i] != None: | ||||||||||||
returnArr.append(self.store[i]) | ||||||||||||
|
||||||||||||
for i in range (0, self.front): | ||||||||||||
if self.store[i] != None: | ||||||||||||
returnArr.append(self.store[i]) | ||||||||||||
else: | ||||||||||||
for i in range (0, self.rear + 1): | ||||||||||||
if self.store[i] != None: | ||||||||||||
returnArr.append(self.store[i]) | ||||||||||||
|
||||||||||||
returnString = '[' + ', '.join(str(x) for x in returnArr) + ']' | ||||||||||||
|
||||||||||||
return returnString |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,6 +4,9 @@ class StackEmptyException(Exception): | |||||||||||||||
pass | ||||||||||||||||
|
||||||||||||||||
class Stack: | ||||||||||||||||
#This stack implementation overloads the LinkedList underneath it | ||||||||||||||||
#to make life easy. In true software dev fashion, no need to | ||||||||||||||||
#reinvent the wheel. | ||||||||||||||||
|
||||||||||||||||
def __init__(self): | ||||||||||||||||
self.store = LinkedList() | ||||||||||||||||
|
@@ -12,7 +15,8 @@ def push(self, element): | |||||||||||||||
""" Adds an element to the top of the Stack. | ||||||||||||||||
Returns None | ||||||||||||||||
""" | ||||||||||||||||
pass | ||||||||||||||||
return self.store.add_first(element) | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ |
||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
def pop(self): | ||||||||||||||||
""" Removes an element from the top | ||||||||||||||||
|
@@ -21,18 +25,25 @@ def pop(self): | |||||||||||||||
The Stack is empty. | ||||||||||||||||
returns None | ||||||||||||||||
""" | ||||||||||||||||
pass | ||||||||||||||||
if self.store.length == 0: | ||||||||||||||||
raise StackEmptyException ("stack is empty") | ||||||||||||||||
|
||||||||||||||||
return self.store.remove_first() | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||||||||||||
self.store.length = 0 | ||||||||||||||||
if self.store.length == 0: | ||||||||||||||||
return True | ||||||||||||||||
return False | ||||||||||||||||
Comment on lines
+37
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 I'm not sure this works... it will always return
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, thanks! |
||||||||||||||||
|
||||||||||||||||
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. | ||||||||||||||||
""" | ||||||||||||||||
pass | ||||||||||||||||
return self.store.__str__ | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The You can also do this with other built in operators and functions. For example, you can override the behavior of the
Suggested change
|
||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨