Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Adds stretch for ring_buffer
  • Loading branch information
ethyl2 committed Apr 24, 2020
commit 5e447b32e44918963a5e5b9d1a8d31c6bab6983d
45 changes: 31 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,22 @@ Implement this behavior in the RingBuffer class. RingBuffer has two methods, `ap

_You may not use a Python List in your implementation of the `append` method (except for the stretch goal)_

*Stretch Goal*: Another method of implementing a ring buffer uses an array (Python List) instead of a linked list. What are the advantages and disadvantages of using this method? What disadvantage normally found in arrays is overcome with this arrangement?
_Stretch Goal_: Another method of implementing a ring buffer uses an array (Python List) instead of a linked list. What are the advantages and disadvantages of using this method? What disadvantage normally found in arrays is overcome with this arrangement?

Using an array was much easier for me to visualize and implement, because I didn't have to deal with setting the head and tail. Initializing the array to have the number of elements equal to the capacity made it easy to keep track of the current index and swap values.

Normally, adding to an array could cause more memory to have to be allocated if more memory is needed than what was initially allocated, but in this case, the capacity is fixed, so more memory won't be needed, so that is an advantage of this arrangement.

As far as big O goes, linked lists and arrays have similar values for several methods. However, finding an element in a linked list has O(n) versus an array's O(1), so that is one advantage of an array implementation.

| method | Array | DLL |
| --------- | :----------: | ------------: |
| append | 1 | 1 |
| print nth | 1 | n |
| add front | n | 1 |
| del front | n | 1 |
| del mid | n | 1 (n to find) |
| find | n (or log n) | n |

For example:

Expand Down Expand Up @@ -62,44 +77,46 @@ buffer.get() # should return ['d', 'e', 'f']

#### Task 2. Runtime Optimization

***!Important!*** If you are running this using PowerShell by clicking on the green play button, you will get an error that `names1.txt` is not found. To resolve this, run it, get the error, then `cd` into the `names` directory in the `python` terminal that opens in VSCode.
**_!Important!_** If you are running this using PowerShell by clicking on the green play button, you will get an error that `names1.txt` is not found. To resolve this, run it, get the error, then `cd` into the `names` directory in the `python` terminal that opens in VSCode.

Navigate into the `names` directory. Here you will find two text files containing 10,000 names each, along with a program `names.py` that compares the two files and prints out duplicate name entries. Try running the code with `python3 names.py`. Be patient because it might take a while: approximately six seconds on my laptop. What is the runtime complexity of this code?

Six seconds is an eternity so you've been tasked with speeding up the code. Can you get the runtime to under a second? Under one hundredth of a second?

*You may not use the built in Python list, set, or dictionary in your solution for this problem. However, you can and should use the provided `duplicates` list to return your solution.*
_You may not use the built in Python list, set, or dictionary in your solution for this problem. However, you can and should use the provided `duplicates` list to return your solution._

(Hint: You might try importing a data structure you built during the week)


#### Task 3. Reverse a Linked List Recursively

Inside of the `reverse` directory, you'll find a basic implementation of a Singly Linked List. _Without_ making it a Doubly Linked List (adding a tail attribute), complete the `reverse_list()` function within `reverse/reverse.py` reverse the contents of the list using recursion, *not a loop.*
Inside of the `reverse` directory, you'll find a basic implementation of a Singly Linked List. _Without_ making it a Doubly Linked List (adding a tail attribute), complete the `reverse_list()` function within `reverse/reverse.py` reverse the contents of the list using recursion, _not a loop._

For example,

```
1->2->3->None
```

would become...

```
3->2->1->None
```

While credit will be given for a functional solution, only optimal solutions will earn a ***3*** on this task.
While credit will be given for a functional solution, only optimal solutions will earn a **_3_** on this task.

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

### Rubric
| OBJECTIVE | TASK | 1 - DOES NOT MEET Expectations | 2 - MEETS Expectations | 3 - EXCEEDS Expectations | SCORE |
| ---------- | ----- | ------- | ------- | ------- | -- |
| _Student should be able to construct a queue and stack and justify the decision to use a linked list instead of an array._ | Task 1. Implement a Ring Buffer Data Structure | Solution in `ring_buffer.py` DOES NOT run OR it runs but has multiple logical errors, failing 3 or more tests | Solution in `ring_buffer.py` runs, but may have one or two logical errors; passes at least 9/11 tests (Note that each _assert_ function is a test.) | Solution in `ring_buffer.py` has no syntax or logical errors and passes 11/11 tests (Note that each _assert_ function is a test.)| |
| _Student should be able to construct a binary search tree class that can perform basic operations with O(log n) runtime._ | Task 2. Runtime Optimization | Student does NOT correctly identify the runtime of the starter code in `name.py` and optimize it to run in under 6 seconds | Student does not identify the runtime of the starter code in `name.py`, but optimizes it to run in under 6 seconds, with a solution of O(n log n) or better | Student does BOTH correctly identify the runtime of the starter code in `name.py` and optimizes it to run in under 6 seconds, with a solution of 0(n log n) or better | |
| _Student should be able to construct a linked list and compare the runtime of operations to an array to make the optimal choice between them._ | Task 3. Reverse the contents of a Singly Linked List using Recursion| Student's solution in `reverse.py` is failing one or more tests | Student's solution in `reverse.py` is able to correctly print out the contents of the Linked List in reverse order, passing all tests, BUT, the runtime of their solution is not optimal (requires looping through the list more than once) | Student's solution in `reverse.py` is able to correctly print out the contents of the Linked List in reverse order, passing all tests AND it has a runtime of O(n) or better | |

| OBJECTIVE | TASK | 1 - DOES NOT MEET Expectations | 2 - MEETS Expectations | 3 - EXCEEDS Expectations | SCORE |
| ---------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- |
| _Student should be able to construct a queue and stack and justify the decision to use a linked list instead of an array._ | Task 1. Implement a Ring Buffer Data Structure | Solution in `ring_buffer.py` DOES NOT run OR it runs but has multiple logical errors, failing 3 or more tests | Solution in `ring_buffer.py` runs, but may have one or two logical errors; passes at least 9/11 tests (Note that each _assert_ function is a test.) | Solution in `ring_buffer.py` has no syntax or logical errors and passes 11/11 tests (Note that each _assert_ function is a test.) | |
| _Student should be able to construct a binary search tree class that can perform basic operations with O(log n) runtime._ | Task 2. Runtime Optimization | Student does NOT correctly identify the runtime of the starter code in `name.py` and optimize it to run in under 6 seconds | Student does not identify the runtime of the starter code in `name.py`, but optimizes it to run in under 6 seconds, with a solution of O(n log n) or better | Student does BOTH correctly identify the runtime of the starter code in `name.py` and optimizes it to run in under 6 seconds, with a solution of 0(n log n) or better | |
| _Student should be able to construct a linked list and compare the runtime of operations to an array to make the optimal choice between them._ | Task 3. Reverse the contents of a Singly Linked List using Recursion | Student's solution in `reverse.py` is failing one or more tests | Student's solution in `reverse.py` is able to correctly print out the contents of the Linked List in reverse order, passing all tests, BUT, the runtime of their solution is not optimal (requires looping through the list more than once) | Student's solution in `reverse.py` is able to correctly print out the contents of the Linked List in reverse order, passing all tests AND it has a runtime of O(n) or better | |

#### Passing the Sprint

Score ranges for a 1, 2, and 3 are shown in the rubric above. For a student to have _passed_ a sprint challenge, they need to earn an **average of at least 2** for all items on the rubric.
22 changes: 19 additions & 3 deletions ring_buffer/ring_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,26 @@ def get(self):

class ArrayRingBuffer:
def __init__(self, capacity):
pass
self.capacity = capacity
self.current = None
self.storage = [None for i in range(0, capacity)]

def append(self, item):
pass
print(self.storage)
if self.current is None:
self.storage[0] = item
self.current = 1
else:
self.storage[self.current] = item
self.current += 1
if self.current == self.capacity:
self.current = 0
print(self.storage)
print('\n')

def get(self):
pass
list_buffer_contents = []
for item in self.storage:
if item is not None:
list_buffer_contents.append(item)
return list_buffer_contents
6 changes: 3 additions & 3 deletions ring_buffer/test_ring_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_ring_buffer(self):
self.buffer_2.append(i)
self.assertEqual(self.buffer_2.get(), [45, 46, 47, 48, 49])

'''

class ArrayRingBufferTests(unittest.TestCase):
def setUp(self):
self.buffer = ArrayRingBuffer(5)
Expand All @@ -55,10 +55,12 @@ def test__array_ring_buffer(self):
self.buffer.append('c')
self.buffer.append('d')
self.assertEqual(len(self.buffer.storage), 5)

self.assertEqual(self.buffer.get(), ['a', 'b', 'c', 'd'])

self.buffer.append('e')
self.assertEqual(len(self.buffer.storage), 5)

self.assertEqual(self.buffer.get(), ['a', 'b', 'c', 'd', 'e'])

self.buffer.append('f')
Expand All @@ -75,8 +77,6 @@ def test__array_ring_buffer(self):
self.buffer_2.append(i)
self.assertEqual(self.buffer_2.get(), [45, 46, 47, 48, 49])

'''


if __name__ == '__main__':
unittest.main()