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

Roza-Cedar, re-submission including comprehension questions #38

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
19 changes: 10 additions & 9 deletions stacks_queues/linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ def __init__(self):
# Space Complexity O(1)
def add_first(self, value):
new_node = Node(value)
new_node.next = self.head

if self.head:
old_head = self.head
self.head.previous = new_node
self.head = new_node
self.head = new_node
new_node.next = old_head
else:
self.head = new_node
if not self.tail:
self.tail = self.head

Expand Down Expand Up @@ -240,16 +242,15 @@ def get_last(self):

def __str__(self):
values = []

current = self.head
while current:
values.append(str(current.value))
current = current.next

return ", ".join(values)

ll = LinkedList()
ll.add_first(5)
ll.add_first(25)
ll.add_last(1)
print(ll)
# ll = LinkedList()
# ll.add_first(5)
# ll.add_first(25)
# ll.add_last(1)
# print(ll)
47 changes: 40 additions & 7 deletions stacks_queues/queue.py
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):
Expand All @@ -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


Expand All @@ -23,39 +27,68 @@ def enqueue(self, element):
In the store are occupied
returns None
"""
pass
if self.size == self.buffer_size:

Choose a reason for hiding this comment

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

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():

Choose a reason for hiding this comment

The 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]

Choose a reason for hiding this comment

The 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

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 Queue is empty
And False otherwise.
"""
pass
return self.front == self.rear

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

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

I would recommend using self.buffer_size instead of the constants for style reasons. It will also allow us to expand our queue functionality in the future, say if we wanted to increase buffer_size instead of just raising an error.

Suggested change
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])
while (start_index % self.buffer_size) < self.buffer_size and len(queue_list) < self.size:
if self.store[start_index % self.buffer_size] is not None:
queue_list.append(self.store[start_index % self.buffer_size])

start_index += 1
else:
start_index += 1
return str(queue_list)


18 changes: 13 additions & 5 deletions stacks_queues/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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
"""
pass

return self.store.remove_first()

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):

Choose a reason for hiding this comment

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

✨ This works, but you might also consider taking advantage of LinkedList's str() method

""" 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)