Skip to content

Commit 76f960f

Browse files
Merge pull request #1 from Dstar4/daniel-starling
daniel-starling
2 parents 1262f36 + e6015f2 commit 76f960f

File tree

4 files changed

+66
-23
lines changed

4 files changed

+66
-23
lines changed

Data_Structures_Answers.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
Add your answers to the questions below.
22

33
1. What is the runtime complexity of your ring buffer's `append` method?
4-
4+
O(1)
55
2. What is the space complexity of your ring buffer's `append` function?
6-
6+
O(1)
77
3. What is the runtime complexity of your ring buffer's `get` method?
8-
8+
O(n)
99
4. What is the space complexity of your ring buffer's `get` method?
10-
10+
O(n)
1111

1212
5. What is the runtime complexity of the provided code in `names.py`?
13-
13+
O(n^2) - nested for loop
1414
6. What is the space complexity of the provided code in `names.py`?
15-
15+
O(n)
1616
7. What is the runtime complexity of your optimized code in `names.py`?
17-
17+
O(n)
1818
8. What is the space complexity of your optimized code in `names.py`?
19+
O(1)

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Sprint Challenge: Data Structures
22

3+
34
In this week's Sprint you implemented some classic and fundamental data structures and learned about how to go about evaluating their respective runtimes and performance. This Sprint Challenge aims to assess your comfort with these topics through exercises that build on the data structures you implemented and the algorithmic intuition you've started to build up.
45

56
## Instructions
@@ -70,7 +71,7 @@ Open up the `Data_Structures_Answers.md` file. This is where you'll jot down you
7071

7172
### Stretch Problems
7273

73-
1. 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)
74+
1. 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)
7475

7576

7677
### Rubric

names/names.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,29 @@
1111
f.close()
1212

1313
duplicates = []
14-
for name_1 in names_1:
15-
for name_2 in names_2:
16-
if name_1 == name_2:
17-
duplicates.append(name_1)
1814

19-
end_time = time.time()
20-
print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
21-
print (f"runtime: {end_time - start_time} seconds")
15+
# loops through names 2 for every name in name_1
16+
# loops through 10,000 names 10,000 times
17+
18+
# for name_1 in names_1:
19+
# for name_2 in names_2:
20+
# if name_1 == name_2:
21+
# duplicates.append(name_1)
22+
23+
# runtime: 6.5317909717559814 seconds
24+
25+
# loops through names_2 and checks if it is in names_1_set
26+
# sets have no index or slicing and can hold any type of data
27+
# membership tests are faster in a set because they use a hash table
28+
# so a set will reduce one of our for loops to a O(1) operation
2229

30+
names_1_set = set(names_1)
31+
for name in names_2:
32+
if name in names_1_set:
33+
duplicates.append(name)
34+
35+
# runtime: 0.003815174102783203 seconds
36+
37+
end_time = time.time()
38+
print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
39+
print(f"runtime: {end_time - start_time} seconds")

ring_buffer/ring_buffer.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
1+
# buffer does not grow
2+
# last element is replaced by new element
3+
4+
15
class RingBuffer:
2-
def __init__(self, capacity):
3-
self.capacity = capacity
4-
self.current = 0
5-
self.storage = [None]*capacity
6+
def __init__(self, capacity):
7+
self.capacity = capacity
8+
self.current = 0
9+
self.storage = [None]*capacity
10+
11+
# adds a new element and deletes the last element if length == capacity
12+
def append(self, item):
13+
print(self.current)
14+
print(self.storage)
15+
16+
# replaces the item for the "current" index
17+
self.storage[self.current] = item
18+
# check that our current position is not beyond the lists length
19+
if self.current < self.capacity - 1:
20+
# increment our position
21+
self.current += 1
22+
# if we are past the length of the array set to index[0]
23+
else:
24+
self.current = 0
625

7-
def append(self, item):
8-
pass
26+
# returns all of the elements in the buffer
27+
# does not return None values
928

10-
def get(self):
11-
pass
29+
def get(self):
30+
returnArr = []
31+
for i in self.storage:
32+
if i is not None:
33+
returnArr.append(i)
34+
print(returnArr)
35+
return returnArr

0 commit comments

Comments
 (0)