Skip to content

first push #423

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
25 changes: 14 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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.

## Instructions
## Instructions##

**Read these instructions carefully. Understand exactly what is expected _before_ starting this Sprint Challenge.**

Expand Down Expand Up @@ -58,7 +58,7 @@ 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?

Expand All @@ -71,28 +71,31 @@ A follow-up question to think about: _*once you've used one of the data structur
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`.

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
| TASK | 1 - DOES NOT MEET Expectations | 2 - MEETS Expectations | 3 - EXCEEDS Expectations | SCORE |
| ----- | ------- | ------- | ------- | -- |
| 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 2 or more tests. | Solution in `ring_buffer.py` runs, but may have one or two logical errors; passes at least 5/6 tests (Note that each function in the test file that begins with `test` is a test). | Solution in `ring_buffer.py` has no syntax or logical errors and passes all tests (Note that each function in the test file that begins with `test` is a test). | |
| Task 2. Runtime Optimization | Student does NOT correctly identify the runtime of the starter code in `name.py` and is not able to optimize it to run in under 6 seconds using a data structure that was implemented during the week. | 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 that exhibits the appropriate runtime, using a data structure that was implemented during the week | Student does BOTH correctly identify the runtime of the starter code in `name.py` and optimizes it to run in under 6 seconds, with an appropriate runtime using a data structure that was implemented during the week. | |
| Task 3. Reverse the contents of a Singly Linked List | 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 exhibits an appropriate runtime. | |

| TASK | 1 - DOES NOT MEET Expectations | 2 - MEETS Expectations | 3 - EXCEEDS Expectations | SCORE |
| ---------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- |
| 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 2 or more tests. | Solution in `ring_buffer.py` runs, but may have one or two logical errors; passes at least 5/6 tests (Note that each function in the test file that begins with `test` is a test). | Solution in `ring_buffer.py` has no syntax or logical errors and passes all tests (Note that each function in the test file that begins with `test` is a test). | |
| Task 2. Runtime Optimization | Student does NOT correctly identify the runtime of the starter code in `name.py` and is not able to optimize it to run in under 6 seconds using a data structure that was implemented during the week. | 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 that exhibits the appropriate runtime, using a data structure that was implemented during the week | Student does BOTH correctly identify the runtime of the starter code in `name.py` and optimizes it to run in under 6 seconds, with an appropriate runtime using a data structure that was implemented during the week. | |
| Task 3. Reverse the contents of a Singly Linked List | 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 exhibits an appropriate runtime. | |

#### 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 **at least 2** for all items on the rubric.
77 changes: 77 additions & 0 deletions names/binary_search_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
''' things to remember. if you want to:
delete - you must traverse
on delete - smallest child becomes the parent
>= flows right '''


class BinarySearchTree:
def __init__(self, value):
self.value = value
self.left = None
self.right = None

''' on insert, it adds the input value to the BST
it follows the rules of the order of elements
in order to find your insertion spot, you need to traverse'''

# Insert the given value into the tree
def insert(self, value):
if value < self.value:
if self.left == None:
self.left = BinarySearchTree(value)
else:
self.left.insert(value)
else:
if self.right == None:
self.right = BinarySearchTree(value)
else:
self.right.insert(value)

''' 'contains' will search the BST for the input value
a boolean will be returned to confirm the value exists or not
Traverse the tree by starting at the root
On finding the first instance of the value - stop (return true)
if the search reaches a node that doesn't have children - then the value is not found (reutrn false)'''

# Return True if the tree contains the value
# False if it does not
def contains(self, target):
if self.value == target:
return True
else:

if target < self.value:
if self.left == None:
return False
else:
return self.left.contains(target)
else:
if self.right == None:
return False
else:
return self.right.contains(target)

''' 'get max' will return max value in the BST
search moves right until it can go no further'''

# Return the maximum value found in the tree
def get_max(self):
if self.right == None:
return self.value
else:
return self.right.get_max()

''' 'for each' traverses every node in the tree
executes the passed-in callback func on each node '''

# Call the function `cb` on the value of each node
# You may use a recursive or iterative approach
def for_each(self, cb):

cb(self.value)

if self.right != None:
self.right.for_each(cb)

if self.left != None:
self.left.for_each(cb)
35 changes: 29 additions & 6 deletions names/names.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import time

start_time = time.time()
from binary_search_tree import BinarySearchTree

f = open('names_1.txt', 'r')
names_1 = f.read().split("\n") # List containing 10000 names
Expand All @@ -10,17 +9,41 @@
names_2 = f.read().split("\n") # List containing 10000 names
f.close()

duplicates = [] # Return the list of duplicates in this data structure
start_time = time.time()
duplicates = []


# Lambda initial starter Code

print('\n Lambda slow poke code')

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


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


# My code
print("Under 1 second vroom vroom code")
start_time2 = time.time()

bst = BinarySearchTree(names_1[0])
bstduplicates = []
for name in names_1:
bst.insert(name)

for name in names_2:
if bst.contains(name):
bstduplicates.append(name)

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(bstduplicates)} duplicates:\n\n{', '.join(bstduplicates)}\n\n")
print(f"runtime: {end_time - start_time2} seconds")

# ---------- Stretch Goal -----------
# Python has built-in tools that allow for a very efficient approach to this problem
Expand Down
28 changes: 21 additions & 7 deletions reverse/reverse.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class Node:
def __init__(self, value=None, next_node=None):
# the value at this linked list node
self.value = value
# reference to the next node in the list
self.next_node = next_node

def get_value(self):
Expand All @@ -10,15 +12,17 @@ def get_next(self):
return self.next_node

def set_next(self, new_next):
# set this node's next_node reference to the passed in node
self.next_node = new_next


class LinkedList:
def __init__(self):
# reference to the head of the list
self.head = None

def add_to_head(self, value):
node = Node(value)

if self.head is not None:
node.set_next(self.head)

Expand All @@ -27,16 +31,26 @@ def add_to_head(self, value):
def contains(self, value):
if not self.head:
return False

# get a reference to the node we're currently at; update this as we traverse the list
current = self.head

# check to see if we're at a valid node
while current:
# return True if the current value we're looking at matches our target value
if current.get_value() == value:
return True

# update our current node to the current node's next node
current = current.get_next()

# if we've gotten here, then the target node isn't in our list
return False

def reverse_list(self, node, prev):
pass
def reverse_list(self):
prev = None
current = self.head

while current is not None:
next = current.next_node
current.next_node = prev
prev = current
current = next

self.head = prev
80 changes: 41 additions & 39 deletions reverse/test_reverse.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
import unittest
from reverse import LinkedList


class LinkedListTests(unittest.TestCase):
def setUp(self):
self.list = LinkedList()

def test_add_to_head(self):
self.list.add_to_head(1)
self.assertEqual(self.list.head.value, 1)
self.list.add_to_head(2)
self.assertEqual(self.list.head.value, 2)

def test_contains(self):
self.list.add_to_head(1)
self.list.add_to_head(2)
self.list.add_to_head(10)
self.assertTrue(self.list.contains(2))
self.assertTrue(self.list.contains(10))
self.assertFalse(self.list.contains(1000))

def test_empty_reverse(self):
self.list.reverse_list(self.list.head, None)
self.assertEqual(self.list.head, None)

def test_single_reverse(self):
self.list.add_to_head(1)
self.list.reverse_list(self.list.head, None)
self.assertEqual(self.list.head.value, 1)

def test_longer_reverse(self):
self.list.add_to_head(1)
self.list.add_to_head(2)
self.list.add_to_head(3)
self.list.add_to_head(4)
self.list.add_to_head(5)
self.assertEqual(self.list.head.value, 5)
self.list.reverse_list(self.list.head, None)
self.assertEqual(self.list.head.value, 1)
self.assertEqual(self.list.head.get_next().value, 2)
self.assertEqual(self.list.head.get_next().get_next().value, 3)

def setUp(self):
self.list = LinkedList()

def test_add_to_head(self):
self.list.add_to_head(1)
self.assertEqual(self.list.head.value, 1)
self.list.add_to_head(2)
self.assertEqual(self.list.head.value, 2)

def test_contains(self):
self.list.add_to_head(1)
self.list.add_to_head(2)
self.list.add_to_head(10)
self.assertTrue(self.list.contains(2))
self.assertTrue(self.list.contains(10))
self.assertFalse(self.list.contains(1000))

def test_empty_reverse(self):
self.list.reverse_list()
self.assertEqual(self.list.head, None)

def test_single_reverse(self):
self.list.add_to_head(1)
self.list.reverse_list()
self.assertEqual(self.list.head.value, 1)

def test_longer_reverse(self):
self.list.add_to_head(1)
self.list.add_to_head(2)
self.list.add_to_head(3)
self.list.add_to_head(4)
self.list.add_to_head(5)
self.assertEqual(self.list.head.value, 5)
self.list.reverse_list()
self.assertEqual(self.list.head.value, 1)
self.assertEqual(self.list.head.get_next().value, 2)
self.assertEqual(self.list.head.get_next().get_next().value, 3)


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