Skip to content

Commit f4b541f

Browse files
author
Elissa
committed
Adding Linked List task
1 parent 2cfbbaa commit f4b541f

File tree

4 files changed

+109
-46
lines changed

4 files changed

+109
-46
lines changed

Data_Structures_Answers.md

Lines changed: 0 additions & 18 deletions
This file was deleted.

README.md

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ In this week's Sprint you implemented some classic and fundamental data structur
88

99
This is an individual assessment. All work must be your own. Your Challenge score is a measure of your ability to work independently using the material covered throughout this sprint. You need to demonstrate proficiency in the concepts and objectives that were introduced and that you practiced in the preceding days.
1010

11-
You are not allowed to collaborate during the Sprint Challenge. However, you are encouraged to follow the twenty-minute rule and seek support from your PM and Instructor in your cohort help channel on Slack. Your submitted work reflects your proficiency in the concepts and topics that were covered this week.
11+
You are not allowed to collaborate during the Sprint Challenge. However, you are encouraged to follow the twenty-minute rule and seek support from your TL and Instructor in your cohort help channel on Slack. Your submitted work reflects your proficiency in the concepts and topics that were covered this week.
1212

1313
You have three hours to complete this Sprint Challenge. Plan your time accordingly.
1414

@@ -22,7 +22,7 @@ This Sprint Challenge is split into three parts:
2222

2323
1. Implement a data structure called a ring buffer (more details below)
2424
2. Optimizing some inefficient code
25-
3. Analyzing time and space complexities from parts 1 and 2
25+
3. Reversing the contents of a singly linked list
2626

2727
### Minimum Viable Product
2828

@@ -68,37 +68,24 @@ Six seconds is an eternity so you've been tasked with speeding up the code. Can
6868

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

71-
#### Task 3. Analyze Some Runtimes
7271

73-
Open up the `Data_Structures_Answers.md` file. This is where you'll jot down your answers for the runtimes of the functions/data structures you just implemented. Also include the runtime and space complexities of the original code and your optimized solution from `names.py`.
72+
#### Task 3. Reverse a Linked List
7473

74+
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.
75+
76+
For example,
77+
```
78+
1->2->3->None
79+
```
80+
would become...
81+
```
82+
3->2->1->None
83+
```
84+
85+
While credit will be given for a functional solution, only optimal solutions will earn a ***3*** on this task.
7586
### Stretch Problems
7687

7788
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)
7889

7990

8091
### Rubric
81-
82-
#### Ring Buffer
83-
84-
- Ring buffer implementation passes the tests: 10 points total
85-
86-
#### Names
87-
88-
- Optimize with an O(n log n) runtime solution: 8 points total
89-
- Optimize with an O(n) runtime solution: 10 points total
90-
91-
#### Complexity
92-
93-
- One point each: 8 points total
94-
95-
#### Stretch
96-
97-
- `names.py` is optimized with sub-quadratic runtime complexity and tightly constrained linear space complexity: 4 points
98-
99-
100-
#### Grading
101-
102-
* *3*: 28+
103-
* *2*: 20-27
104-
* *1*: 0-19

reverse/reverse.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Node:
2+
def __init__(self, value=None, next_node=None):
3+
# the value at this linked list node
4+
self.value = value
5+
# reference to the next node in the list
6+
self.next_node = next_node
7+
8+
def get_value(self):
9+
return self.value
10+
11+
def get_next(self):
12+
return self.next_node
13+
14+
def set_next(self, new_next):
15+
# set this node's next_node reference to the passed in node
16+
self.next_node = new_next
17+
18+
class LinkedList:
19+
def __init__(self):
20+
# reference to the head of the list
21+
self.head = None
22+
23+
def add_to_head(self, value):
24+
node = Node(value)
25+
if self.head is not None:
26+
node.set_next(self.head)
27+
28+
self.head = node
29+
30+
def contains(self, value):
31+
if not self.head:
32+
return False
33+
# get a reference to the node we're currently at; update this as we traverse the list
34+
current = self.head
35+
# check to see if we're at a valid node
36+
while current:
37+
# return True if the current value we're looking at matches our target value
38+
if current.get_value() == value:
39+
return True
40+
# update our current node to the current node's next node
41+
current = current.get_next()
42+
# if we've gotten here, then the target node isn't in our list
43+
return False
44+
45+
def reverse_list(self):
46+
# TO BE COMPLETED
47+
pass

reverse/test_reverse.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import unittest
2+
from reverse import LinkedList
3+
4+
class LinkedListTests(unittest.TestCase):
5+
def setUp(self):
6+
self.list = LinkedList()
7+
8+
def test_add_to_head(self):
9+
self.list.add_to_head(1)
10+
self.assertEqual(self.list.head.value, 1)
11+
self.list.add_to_head(2)
12+
self.assertEqual(self.list.head.value, 2)
13+
14+
def test_contains(self):
15+
self.list.add_to_head(1)
16+
self.list.add_to_head(2)
17+
self.list.add_to_head(10)
18+
self.assertTrue(self.list.contains(2))
19+
self.assertTrue(self.list.contains(10))
20+
self.assertFalse(self.list.contains(1000))
21+
22+
def test_empty_reverse(self):
23+
self.list.reverse_list()
24+
self.assertEqual(self.list.head, None)
25+
26+
def test_single_reverse(self):
27+
self.list.add_to_head(1)
28+
self.list.reverse_list()
29+
self.assertEqual(self.list.head.value, 1)
30+
31+
def test_longer_reverse(self):
32+
self.list.add_to_head(1)
33+
self.list.add_to_head(2)
34+
self.list.add_to_head(3)
35+
self.list.add_to_head(4)
36+
self.list.add_to_head(5)
37+
self.assertEqual(self.list.head.value, 5)
38+
self.list.reverse_list()
39+
self.assertEqual(self.list.head.value, 1)
40+
self.assertEqual(self.list.head.get_next().value, 2)
41+
self.assertEqual(self.list.head.get_next().get_next().value, 3)
42+
43+
44+
45+
46+
if __name__ == '__main__':
47+
unittest.main()

0 commit comments

Comments
 (0)