Skip to content

Commit a7e21eb

Browse files
committed
Convert to python
1 parent 25a062c commit a7e21eb

14 files changed

+191
-6119
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.DS_Store
2-
node_modules
2+
node_modules
3+
*.pyc

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
# Sprint Challenge: Data Structures and Algorithms
22

3-
For this sprint challenge, you'll be implementing a few functions that build off of some of the data structures you implemented in the first half of the week. Then you'll be analyzing the runtimes of all of the data structure methods.
3+
For the data structures portion of this sprint challenge, you'll be implementing a few functions that build off of some of the data structures you implemented in the first half of the week. Then you'll be analyzing the runtimes of these functions.
44

55
For the algorithms portion of the sprint challenge, you'll be answering the questions posed in the `exercises.pdf` file regarding runtime complexities and algorithmic paradigms.
66

77
## Data Structures
8-
Before getting started, run `npm install` in the root-level directory to install needed dependencies for testing.
98

109
### Task 1. Implement Depth-First and Breadth-First Traversal on the Binary Search Tree Class
11-
Navigate into the `data_structures` directory. Inside the `src` directory, you'll see the `binary-search-tree.js` file with the completed code for the binary search tree class. Your first task is to implement the methods `depthFirstForEach` and `breadthFirstForEach` on the `BinarySearchTree` class. Think about what data structures you'll need to use in order to achieve the desired order of each respective method:
10+
Navigate into the `ex_1` directory in the `data_structures` directory. Inside, you'll see the `binary-search-tree.py` file with a complete implementation of the binary search tree class. Your first task is to implement the methods `depth_first_for_each` and `breadth_first_for_each` on the `BinarySearchTree` class:
1211

13-
* `depthFirstForEach(cb)` receives a callback function as a parameter. This method iterates over the binary search tree in [depth-first](https://en.wikipedia.org/wiki/Depth-first_search) order, applying the supplied callback function to each tree element in turn.
14-
* `breadthFirstForEach(cb)` receives a callback function as a parameter. This method iterates over the binary search tree in [breadth-first](https://en.wikipedia.org/wiki/Breadth-first_search) order, applying the supplied callback function to each tree element in turn.
12+
* `depth_first_for_each(cb)` receives a callback function as a parameter. This method iterates over the binary search tree in [depth-first](https://en.wikipedia.org/wiki/Depth-first_search) order, applying the supplied callback function to each tree element in turn.
13+
* `breadth_first_for_each(cb)` receives a callback function as a parameter. This method iterates over the binary search tree in [breadth-first](https://en.wikipedia.org/wiki/Breadth-first_search) order, applying the supplied callback function to each tree element in turn.
1514

16-
Run `npm test binary-search-tree` to run the tests for this function to ensure that your implementation is correct.
15+
NOTE: In Python, anonymous functions are referred to as "lambda functions". When passing in a callback function as input to either `depth_first_for_each` or `breadth_first_for_each`, you'll want to define the callbacks as lambda functions. For more information on lambda functions, check out this documentation: [https://docs.python.org/3/tutorial/controlflow.html#lambda-expressions](https://docs.python.org/3/tutorial/controlflow.html#lambda-expressions)
16+
17+
Run `python3 test_binary_search_tree.py` to run the tests for these methods to ensure that your implementation is correct.
1718

1819
### Task 2. Implement Heapsort
19-
Inside the `src` directory you'll also find the `heap.js` file with the completed code for the Heap class that you worked on. Your second task is to implement a sorting method called [heapsort](https://en.wikipedia.org/wiki/Heapsort) that uses the heap data structure in order to sort an array of numbers. Your `heapsort` function should return a new array containing all of the sorted data.
20+
Inside the `ex_2` directory you'll find the `heap.py` file with a working implementation of the heap class. Your second task is to implement a sorting method called [heapsort](https://en.wikipedia.org/wiki/Heapsort) that uses the heap data structure in order to sort an array of numbers. Your `heapsort` function should return a new array containing all of the sorted data.
2021

21-
Run `npm test heap` to run the tests for your `heapsort` function to ensure that your implementation is correct.
22+
Run `python3 test_heap.py` to run the tests for your `heapsort` function to ensure that your implementation is correct.
2223

2324
### Task 3. Analyze some runtimes
2425
Open up the `Data_Structures_Answers.md` file. This is where you'll jot down your answers for the runtimes of the functions you just implemented. Be sure to also answer any other questions posed in the `Answers.md` file!
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Add your answers to the questions below.
2+
3+
1. What is the runtime complexity of your `depth_first_for_each` method?
4+
5+
2. What is the space complexity of your `depth_first_for_each` function?
6+
7+
3. What is the runtime complexity of your `breadth_first_for_each` method?
8+
9+
4. What is the space complexity of your `breadth_first_for_each` method?
10+
11+
5. What is the runtime complexity of your `heapsort` function?
12+
13+
6. What is the space complexity of the `heapsort` function? Recall that your implementation should return a new array with the sorted data. What would be the space complexity if your function instead altered the input array?

data_structures/Data_Structures_Questions.md

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class BinarySearchTree:
2+
def __init__(self, value):
3+
self.value = value
4+
self.left = None
5+
self.right = None
6+
7+
def depth_first_for_each(self, cb):
8+
pass
9+
10+
def breadth_first_for_each(self, cb):
11+
pass
12+
13+
def insert(self, value):
14+
new_tree = BinarySearchTree(value)
15+
if (value < self.value):
16+
if not self.left:
17+
self.left = new_tree
18+
else:
19+
self.left.insert(value)
20+
elif value >= self.value:
21+
if not self.right:
22+
self.right = new_tree
23+
else:
24+
self.right.insert(value)
25+
26+
def contains(self, target):
27+
if self.value == target:
28+
return True
29+
if self.left:
30+
if self.left.contains(target):
31+
return True
32+
if self.right:
33+
if self.right.contains(target):
34+
return True
35+
return False
36+
37+
def get_max(self):
38+
if not self:
39+
return None
40+
max_value = self.value
41+
current = self
42+
while current:
43+
if current.value > max_value:
44+
max_value = current.value
45+
current = current.right
46+
return max_value
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import unittest
2+
from binary_search_tree import BinarySearchTree
3+
4+
class BinarySearchTreeTests(unittest.TestCase):
5+
def setUp(self):
6+
self.bst = BinarySearchTree(5)
7+
8+
def test_depth_first_for_each_executes_callback(self):
9+
arr = []
10+
cb = lambda x: arr.append(x)
11+
12+
self.bst.insert(2)
13+
self.bst.insert(3)
14+
self.bst.insert(7)
15+
self.bst.insert(9)
16+
self.bst.depth_first_for_each(cb)
17+
18+
self.assertEqual(arr, [5, 2, 3, 7, 9])
19+
20+
def test_breadth_first_for_each_executes_callback(self):
21+
arr = []
22+
cb = lambda x: arr.append(x)
23+
24+
self.bst.insert(3)
25+
self.bst.insert(4)
26+
self.bst.insert(10)
27+
self.bst.insert(9)
28+
self.bst.insert(11)
29+
self.bst.breadth_first_for_each(cb)
30+
31+
self.assertEqual(arr, [5, 3, 10, 4, 9, 11])
32+
33+
if __name__ == '__main__':
34+
unittest.main()

data_structures/ex_2/heap.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from math import floor
2+
3+
def heapsort(arr):
4+
pass
5+
6+
class Heap:
7+
def __init__(self):
8+
self.storage = []
9+
10+
def insert(self, value):
11+
self.storage.append(value)
12+
self._bubble_up(len(self.storage) - 1)
13+
14+
def delete(self):
15+
if not len(self.storage):
16+
return None
17+
if len(self.storage) == 1:
18+
return self.storage.pop()
19+
max_value = self.storage[0]
20+
self.storage[0] = self.storage.pop()
21+
self._sift_down(0)
22+
return max_value
23+
24+
def get_max(self):
25+
return self.storage[0]
26+
27+
def get_size(self):
28+
return len(self.storage)
29+
30+
def _bubble_up(self, index):
31+
parent_index = floor((index - 1) / 2) if index > 0 else 0
32+
if self.storage[parent_index] < self.storage[index]:
33+
self.storage[parent_index], self.storage[index] = self.storage[index], self.storage[parent_index]
34+
self._bubble_up(parent_index)
35+
36+
def _sift_down(self, index):
37+
left_index = index * 2 + 1
38+
right_index = index * 2 + 2
39+
max_child_index = len(self.storage) - 1
40+
41+
try:
42+
left_child = self.storage[left_index]
43+
except IndexError:
44+
left_child = None
45+
46+
try:
47+
right_child = self.storage[right_index]
48+
except IndexError:
49+
right_child = None
50+
51+
if left_child and right_child:
52+
max_child_index = left_index if left_child > right_child else right_index
53+
elif left_child:
54+
max_child_index = left_index
55+
elif right_child:
56+
max_child_index = right_child
57+
58+
if self.storage[index] < self.storage[max_child_index]:
59+
self.storage[max_child_index], self.storage[index] = self.storage[index], self.storage[max_child_index]
60+
self._sift_down(max_child_index)

data_structures/ex_2/test_heap.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import unittest
2+
from random import randint
3+
from heap import heapsort
4+
5+
def gen_random_input(length, max):
6+
input = []
7+
for i in range(length):
8+
input.append(randint(0, max))
9+
return input
10+
11+
def is_sorted(arr):
12+
for i in range(len(arr) - 1):
13+
if arr[i] > arr[i+1]:
14+
return False
15+
return True
16+
17+
class HeapTests(unittest.TestCase):
18+
def test_heap_sort_correctness(self):
19+
length = randint(100, 1000)
20+
input = gen_random_input(length, 1000)
21+
output = heapsort(input)
22+
23+
self.assertEqual(len(output), length)
24+
self.assertTrue(is_sorted(output))
25+
26+
if __name__ == '__main__':
27+
unittest.main()

data_structures/src/binary-search-tree.js

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

data_structures/src/heap.js

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

0 commit comments

Comments
 (0)