-
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
Roza-Cedar, re-submission including comprehension questions #38
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 | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,4 +1,7 @@ | ||||||||||||||
|
||||||||||||||
from tracemalloc import start | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
INITIAL_QUEUE_SIZE = 20 | ||||||||||||||
|
||||||||||||||
class QueueFullException(Exception): | ||||||||||||||
|
@@ -13,7 +16,8 @@ def __init__(self): | |||||||||||||
self.store = [None] * INITIAL_QUEUE_SIZE | ||||||||||||||
self.buffer_size = INITIAL_QUEUE_SIZE | ||||||||||||||
self.front = -1 | ||||||||||||||
self.rear = -1 | ||||||||||||||
self.rear = -1 | ||||||||||||||
# -1 is just a marker to indicate nothing in the list, we can mark as None as well | ||||||||||||||
self.size = 0 | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
|
@@ -23,39 +27,68 @@ 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 | ||||||||||||||
self.store[self.rear] = element #insert where rear and front were pointing at first. But rear is the one moving to indicate the position of insert | ||||||||||||||
self.rear = (self.rear + 1) % self.buffer_size #the next insert position | ||||||||||||||
self.size += 1 | ||||||||||||||
|
||||||||||||||
def dequeue(self): | ||||||||||||||
""" Removes and returns an element from the Queue | ||||||||||||||
Raises a QueueEmptyException if | ||||||||||||||
The Queue is empty. | ||||||||||||||
""" | ||||||||||||||
pass | ||||||||||||||
if self.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. ✨ |
||||||||||||||
raise QueueEmptyException | ||||||||||||||
front_element = self.store[self.front] | ||||||||||||||
self.store[self.front] = None | ||||||||||||||
self.front = (self.front + 1) % self.buffer_size | ||||||||||||||
self.size -= 1 | ||||||||||||||
return front_element | ||||||||||||||
# check if empty, if so raise an exception | ||||||||||||||
# find and store the front element | ||||||||||||||
# move front to the next index | ||||||||||||||
# return the old front elements | ||||||||||||||
# front and rear are indexes, like 0, 1 | ||||||||||||||
|
||||||||||||||
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 | ||||||||||||||
return self.front == self.rear | ||||||||||||||
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. 👀 This will check if the queue is empty, but it could also mean the queue |
||||||||||||||
|
||||||||||||||
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 | ||||||||||||||
queue_list = [] | ||||||||||||||
start_index = self.front | ||||||||||||||
while (start_index % INITIAL_QUEUE_SIZE) < INITIAL_QUEUE_SIZE and len(queue_list) < self.size: | ||||||||||||||
if self.store[start_index % INITIAL_QUEUE_SIZE] is not None: | ||||||||||||||
queue_list.append(self.store[start_index % INITIAL_QUEUE_SIZE]) | ||||||||||||||
Comment on lines
+86
to
+88
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 would recommend using
Suggested change
|
||||||||||||||
start_index += 1 | ||||||||||||||
else: | ||||||||||||||
start_index += 1 | ||||||||||||||
return str(queue_list) | ||||||||||||||
|
||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,27 +12,35 @@ def push(self, element): | |
""" Adds an element to the top of the Stack. | ||
Returns None | ||
""" | ||
pass | ||
|
||
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 | ||
Of the Stack | ||
Raises a StackEmptyException if | ||
The Stack is empty. | ||
returns None | ||
""" | ||
pass | ||
|
||
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 | ||
return self.store.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. ✨ |
||
|
||
def __str__(self): | ||
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. ✨ This works, but you might also consider taking advantage of |
||
""" 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 | ||
current = self.store.head | ||
storeStacks = [] | ||
while current: | ||
storeStacks.append(str(current.value)) | ||
current = current.next | ||
return ", ".join(storeStacks) | ||
|
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.
✨