Skip to content

Alexis aros #439

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 3 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ While credit will be given for a functional solution, only optimal solutions wil

#### Stretch

* Say your code from `names.py` is to run on an embedded computer with very limited RAM. Because of this, memory is extremely constrained and you are only allowed to store names in arrays (i.e. Python lists). How would you go about optimizing the code under these conditions? Try it out and compare your solution to the original runtime. (If this solution is less efficient than your original solution, include both and label the strech solution with a comment)
* Say your code from `names.py` is to run on an embedded computer with very limited RAM. Because of this, memory is extremely constrained and you are only allowed to store names in arrays (i.e. Python lists). How would you go about optimizing the code under these conditions? Try it out and compare your solution to the original runtime. (If this solution is less efficient than your original solution, include both and label the stretch solution with a comment)


### Rubric
Expand Down
44 changes: 39 additions & 5 deletions names/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,50 @@
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)
class BSTNode:
def __init__(self, value):
self.value = value
self.left = None
self.right = None

def insert(self, value):
if value < self.value:
if not self.left:
self.left = BSTNode(value)
else:
self.left.insert(value)
else:
if not self.right:
self.right = BSTNode(value)
else:
self.right.insert(value)

def contains(self, target):
if self.value == target:
return True
if target < self.value:
if not self.left:
return False
else:
return self.left.contains(target)
else:
if not self.right:
return False
else:
return self.right.contains(target)

bst = BSTNode(names_1[0])
for names1 in names_1:
bst.insert(names1)
for names2 in names_2:
if bst.contains(names2):
duplicates.append(names2)

end_time = time.time()
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
# What's the best time you can accomplish? Thare are no restrictions on techniques or data
# What's the best time you can accomplish? There are no restrictions on techniques or data
# structures, but you may not import any additional libraries that you did not write yourself.
8 changes: 7 additions & 1 deletion reverse/reverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,10 @@ def contains(self, value):
return False

def reverse_list(self, node, prev):
pass
while node:
reverse_node = node.next_node
node.next_node = prev
prev = node
node = reverse_node
self.head = prev
return node
14 changes: 10 additions & 4 deletions ring_buffer/ring_buffer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
class RingBuffer:
def __init__(self, capacity):
pass
self.capacity = capacity
self.index = -1
self.storage = []

def append(self, item):
pass

if len(self.storage) == self.capacity:
self.index = (self.index + 1) % self.capacity
self.storage[self.index] = item
else:
self.storage.append(item)

def get(self):
pass
return self.storage