Skip to content

MVP met #437

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

Open
wants to merge 6 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
51 changes: 45 additions & 6 deletions names/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,53 @@
duplicates = [] # Return the list of duplicates in this data structure

# Replace the nested for loops below with your improvements
for name_1 in names_1:
for name_2 in names_2:
if name_1 == name_2:
duplicates.append(name_1)

# runtime: 3.0849990844726562 seconds
# duplicates = [ name for name in names_1 if name in names_2 ] # Return the list of duplicates in this data structure


# # 12.839998483657837 seconds
# for name_1 in names_1:
# for name_2 in names_2:
# if name_1 == name_2:
# duplicates.append(name_1)


# runtime: 3.0630006790161133 seconds
class Stack:
def __init__(self):
self.size = 0
self.storage = []

def __len__(self):
return self.size

def push(self, value):
self.size += 1
self.storage.append(value)

def pop(self):
if self.size == 0:
return None
self.size -= 1
return self.storage.pop()


# Create a new Stack
search = Stack()

# for every name in the FIRST array (name_1), add that to stack
for name in names_1:
search.push(name)

# for each item in the stack, if it is in the SECOND array(name_2) append to duplicates
for item in search.storage:
if item in names_2:
duplicates.append(item)

end_time = time.time()
print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print (f"runtime: {end_time - start_time} seconds")
print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print(f"runtime: {end_time - start_time} seconds")

# ---------- Stretch Goal -----------
# Python has built-in tools that allow for a very efficient approach to this problem
Expand Down
47 changes: 46 additions & 1 deletion reverse/reverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def get_next(self):
def set_next(self, new_next):
self.next_node = new_next


class LinkedList:
def __init__(self):
self.head = None
Expand All @@ -38,5 +39,49 @@ def contains(self, value):

return False


# # Function to reverse the linked list
# def reverse(self):
# prev = None
# current = self.head
# while(current is not None):
# next = current.next
# current.next = prev
# prev = current
# current = next
# self.head = prev

def reverse_list(self, node, prev):
pass
prev = None
node = self.head
while(node is not None):
next_node = node.next_node
node.next_node = prev
prev = node
node = next_node
self.head = prev

def push(self, new_data):
new_node = Node(new_data)
new_node.next_node = self.head
self.head = new_node

def printList(self):
temp = self.head
while(temp):
print(temp.value),
temp = temp.next_node


# Print test
llist = LinkedList()
llist.push(20)
llist.push(4)
llist.push(15)
llist.push(85)

print("Given Linked List")
llist.printList()
# llist.reverse_list()
# print("\nReversed Linked List")
# llist.printList()
38 changes: 35 additions & 3 deletions ring_buffer/ring_buffer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
class RingBuffer:
def __init__(self, capacity):
pass
# setup max capacity and storage
self.capacity = capacity
self.storage = []
# initialize ring buffer size to zero
self.size = 0
# instantiate head to update
self.head = 0 # to equal oldest element

# https://www.oreilly.com/library/view/python-cookbook/0596001673/ch05s19.html
# class _ _Full:
# """ class that implements a full buffer """
# def append(self, x):
# """ Append an element overwriting the oldest one. """
# self.data[self.cur] = x
# self.cur = (self.cur+1) % self.max

def append(self, item):
pass
# first check if the storage is full
if self.size == self.capacity:
# replace index at self.head
self.storage[self.head] = item

# update head to be oldest item in storage. When this leaves no remainders start back at 0
self.head = (self.head + 1) % self.capacity

# otherwise if storage is not full enqueue item
else:
self.enqueue(item)

def get(self):
pass
# returns all of the elements in the buffer in a list
return self.storage

# Added enqueue to insert values at end of RingBuffer.
def enqueue(self, value):
# inserts the value at the end of the storage
self.storage.append(value)
# increases size by 1
self.size += 1