Skip to content

Commit 584d012

Browse files
authored
Merge pull request #1 from maxdavid/max-david
Max David
2 parents fa4feac + c8232de commit 584d012

File tree

5 files changed

+166
-76
lines changed

5 files changed

+166
-76
lines changed

names/binary_search_tree.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class BinarySearchTree:
2+
def __init__(self, value):
3+
self.value = value
4+
self.left = None
5+
self.right = None
6+
7+
# Insert the given value into the tree
8+
def insert(self, value):
9+
parent = self
10+
node = BinarySearchTree(value)
11+
while True:
12+
if value < parent.value:
13+
if parent.left:
14+
parent = parent.left # traverse down left
15+
else:
16+
parent.left = node
17+
break
18+
else:
19+
if parent.right:
20+
parent = parent.right # traverse down right
21+
else:
22+
parent.right = node
23+
break
24+
25+
# Return True if the tree contains the value
26+
# False if it does not
27+
def contains(self, target):
28+
node = self
29+
while node.value != target:
30+
if target < node.value and node.left:
31+
node = node.left
32+
elif target >= node.value and node.right:
33+
node = node.right
34+
else:
35+
break
36+
return node.value == target
37+
38+
# Return the maximum value found in the tree
39+
def get_max(self):
40+
node = self
41+
while node.right:
42+
node = node.right
43+
return node.value
44+
45+
# Call the function `cb` on the value of each node
46+
# You may use a recursive or iterative approach
47+
def for_each(self, cb):
48+
node = self
49+
cb(node.value)
50+
if node.left:
51+
node.left.for_each(cb)
52+
if node.right:
53+
node.right.for_each(cb)

names/names.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
11
import time
2+
from binary_search_tree import BinarySearchTree
23

34
start_time = time.time()
45

5-
f = open('names_1.txt', 'r')
6+
f = open("names_1.txt", "r")
67
names_1 = f.read().split("\n") # List containing 10000 names
78
f.close()
89

9-
f = open('names_2.txt', 'r')
10+
f = open("names_2.txt", "r")
1011
names_2 = f.read().split("\n") # List containing 10000 names
1112
f.close()
1213

14+
###########
15+
16+
bst = BinarySearchTree(names_1[0])
17+
for name_1 in names_1[1:]:
18+
bst.insert(name_1)
19+
1320
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)
21+
for name_2 in names_2:
22+
if bst.contains(name_2):
23+
duplicates.append(name_2)
24+
25+
# duplicates = []
26+
# for name_1 in names_1:
27+
# for name_2 in names_2:
28+
# if name_1 == name_2:
29+
# duplicates.append(name_1)
1830

1931
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")
32+
print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
33+
print(f"runtime: {end_time - start_time} seconds")
2234

reverse/reverse.py

Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,63 @@
11
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
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
77

8-
def get_value(self):
9-
return self.value
8+
def get_value(self):
9+
return self.value
1010

11-
def get_next(self):
12-
return self.next_node
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
1317

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
1718

1819
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
20+
def __init__(self):
21+
# reference to the head of the list
22+
self.head = None
23+
24+
def add_to_head(self, value):
25+
node = Node(value)
26+
if self.head is not None:
27+
node.set_next(self.head)
28+
29+
self.head = node
30+
31+
def contains(self, value):
32+
if not self.head:
33+
return False
34+
# get a reference to the node we're currently at; update this as we traverse the list
35+
current = self.head
36+
# check to see if we're at a valid node
37+
while current:
38+
# return True if the current value we're looking at matches our target value
39+
if current.get_value() == value:
40+
return True
41+
# update our current node to the current node's next node
42+
current = current.get_next()
43+
# if we've gotten here, then the target node isn't in our list
44+
return False
45+
46+
def reverse_list(self):
47+
# traverse the list, re-assigning next nodes along the way
48+
if not self.head or not self.head.get_next():
49+
return # length is <= 1
50+
51+
current = self.head
52+
next_node = current.get_next()
53+
previous_node = None
54+
55+
while current.get_next():
56+
current.set_next(previous_node)
57+
previous_node = current
58+
current = next_node
59+
next_node = current.get_next()
60+
61+
current.set_next(previous_node)
62+
self.head = current
63+

ring_buffer/ring_buffer.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
from copy import copy
2+
3+
14
class RingBuffer:
2-
def __init__(self, capacity):
3-
self.capacity = capacity
4-
self.current = 0
5-
self.storage = [None]*capacity
5+
def __init__(self, capacity):
6+
self.capacity = capacity
7+
self.current = 0
8+
self.storage = [None] * capacity
9+
10+
def append(self, item):
11+
self.storage[self.current] = copy(item)
12+
if self.current >= self.capacity - 1:
13+
self.current = 0
14+
else:
15+
self.current += 1
616

7-
def append(self, item):
8-
pass
17+
def get(self):
18+
return [item for item in self.storage if item is not None]
919

10-
def get(self):
11-
pass

ring_buffer/test_ring_buffer.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import unittest
22
from ring_buffer import RingBuffer
33

4+
45
class RingBufferTests(unittest.TestCase):
56
def setUp(self):
67
self.buffer = RingBuffer(5)
@@ -9,35 +10,35 @@ def setUp(self):
910
def test_ring_buffer(self):
1011
self.assertEqual(len(self.buffer.storage), 5)
1112

12-
self.buffer.append('a')
13-
self.buffer.append('b')
14-
self.buffer.append('c')
15-
self.buffer.append('d')
13+
self.buffer.append("a")
14+
self.buffer.append("b")
15+
self.buffer.append("c")
16+
self.buffer.append("d")
1617
self.assertEqual(len(self.buffer.storage), 5)
17-
self.assertEqual(self.buffer.get(), ['a', 'b', 'c', 'd'])
18+
self.assertEqual(self.buffer.get(), ["a", "b", "c", "d"])
1819

19-
self.buffer.append('e')
20+
self.buffer.append("e")
2021
self.assertEqual(len(self.buffer.storage), 5)
21-
self.assertEqual(self.buffer.get(), ['a', 'b', 'c', 'd', 'e'])
22+
self.assertEqual(self.buffer.get(), ["a", "b", "c", "d", "e"])
2223

23-
self.buffer.append('f')
24+
self.buffer.append("f")
2425
self.assertEqual(len(self.buffer.storage), 5)
25-
self.assertEqual(self.buffer.get(), ['f', 'b', 'c', 'd', 'e'])
26+
self.assertEqual(self.buffer.get(), ["f", "b", "c", "d", "e"])
2627

27-
self.buffer.append('g')
28-
self.buffer.append('h')
29-
self.buffer.append('i')
28+
self.buffer.append("g")
29+
self.buffer.append("h")
30+
self.buffer.append("i")
3031
self.assertEqual(len(self.buffer.storage), 5)
31-
self.assertEqual(self.buffer.get(), ['f', 'g', 'h', 'i', 'e'])
32-
33-
self.buffer.append('j')
34-
self.buffer.append('k')
35-
self.assertEqual(self.buffer.get(), ['k', 'g', 'h', 'i', 'j'])
36-
32+
self.assertEqual(self.buffer.get(), ["f", "g", "h", "i", "e"])
33+
34+
self.buffer.append("j")
35+
self.buffer.append("k")
36+
self.assertEqual(self.buffer.get(), ["k", "g", "h", "i", "j"])
37+
3738
for i in range(50):
3839
self.buffer_2.append(i)
3940
self.assertEqual(self.buffer_2.get(), [45, 46, 47, 48, 49])
4041

4142

42-
if __name__ == '__main__':
43+
if __name__ == "__main__":
4344
unittest.main()

0 commit comments

Comments
 (0)