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 the comprehension questions #16

Open
wants to merge 2 commits 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
21 changes: 11 additions & 10 deletions stacks_queues/linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
class EmptyListError(Exception):
pass

# Defines a node in the singly linked list
# Defines a node in the doubly linked list
class Node:
def __init__(self, value, next_node = None, previous_node = None):
self.value = value
Expand All @@ -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:
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():
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]


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.front == self.rear

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


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