Skip to content

Commit b813485

Browse files
authored
Merge pull request #1 from nedssoft/chinedu-orie
Data structure sprint challenge
2 parents 33ecaaa + 2a25909 commit b813485

File tree

4 files changed

+146
-11
lines changed

4 files changed

+146
-11
lines changed

names/binary_search_tree.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import sys
2+
3+
class BinarySearchTree:
4+
def __init__(self, value):
5+
self.value = value
6+
self.left = None
7+
self.right = None
8+
9+
# Insert the given value into the tree
10+
def insert(self, value):
11+
# LEFT CASE
12+
# check if our new nodes value is less than the current nodes value
13+
if value < self.value:
14+
# does it have a child to the left?
15+
if self.left is not None:
16+
self.left.insert(value)
17+
# place our new node here
18+
# otherwise
19+
else:
20+
self.left = BinarySearchTree(value)
21+
# repeat process for the left
22+
23+
# RIGHT CASE
24+
25+
# check if the new nodes value is greater than or equal to the current nodes value
26+
if value >= self.value:
27+
# does it have a child to the right?
28+
if self.right is not None:
29+
self.right.insert(value)
30+
# place our new node here
31+
# otherwise
32+
else:
33+
self.right = BinarySearchTree(value)
34+
# repeat the process for the right
35+
36+
# Return True if the tree contains the value
37+
# False if it does not
38+
def contains(self, target):
39+
40+
current_value = self.value
41+
if current_value == target:
42+
return True
43+
44+
# Check if the target is less than the current node value
45+
if target < current_value:
46+
47+
# Check if left is not None
48+
if self.left is not None:
49+
50+
# Repeat the process towards the left
51+
return self.left.contains(target)
52+
else:
53+
54+
# It means the target is not at the left
55+
return False
56+
57+
# Let's search the rightnode
58+
if target >= current_value:
59+
60+
# Check if the right is none
61+
if self.right is None:
62+
63+
# Then it is not at the right
64+
return False
65+
else:
66+
return self.right.contains(target)
67+
68+
69+
70+

names/names.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import time
2+
from binary_search_tree import BinarySearchTree
23

34
start_time = time.time()
45

@@ -13,10 +14,30 @@
1314
duplicates = [] # Return the list of duplicates in this data structure
1415

1516
# Replace the nested for loops below with your improvements
16-
for name_1 in names_1:
17-
for name_2 in names_2:
18-
if name_1 == name_2:
19-
duplicates.append(name_1)
17+
# for name_1 in names_1:
18+
# for name_2 in names_2:
19+
# if name_1 == name_2:
20+
# duplicates.append(name_1)
21+
22+
"""
23+
First pass, runtime: 1.120605230331421 seconds
24+
"""
25+
# for i in range(len(names_1)-1):
26+
# if names_1[i] in names_2:
27+
# duplicates.append(names_1[i])
28+
29+
"""
30+
Using BST, runtime: 0.10721707344055176 seconds
31+
"""
32+
# Create a BST with names_2
33+
bst = BinarySearchTree(names_2[0])
34+
for i in range(1, len(names_2)):
35+
bst.insert(names_2[i])
36+
# Loop through names_1 and call bst.contans()
37+
38+
for name in names_1:
39+
if bst.contains(name):
40+
duplicates.append(name)
2041

2142
end_time = time.time()
2243
print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")

reverse/reverse.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,27 @@ def contains(self, value):
4444

4545
def reverse_list(self):
4646
# TO BE COMPLETED
47-
pass
47+
48+
# if the head is None
49+
if self.head is None:
50+
51+
# Return none
52+
return None
53+
54+
# Create an empty node
55+
prev = None
56+
57+
# Assign the head to a current var
58+
current = self.head
59+
60+
# Perform swap
61+
while current:
62+
# Assign the current node next to a var
63+
next_node = current.next_node
64+
# Assign the prev to the next node
65+
current.next_node = prev
66+
prev = current
67+
current = next_node
68+
69+
self.head = prev
70+

ring_buffer/ring_buffer.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
from doubly_linked_list import DoublyLinkedList
22

3-
43
class RingBuffer:
54
def __init__(self, capacity):
65
self.capacity = capacity
76
self.current = None
87
self.storage = DoublyLinkedList()
98

109
def append(self, item):
11-
pass
10+
11+
if (len(self.storage) == self.capacity):
12+
self.current.value = item
13+
if (self.current.next):
14+
self.current = self.current.next
15+
else:
16+
self.current = self.storage.head
17+
else:
18+
self.storage.add_to_tail(item)
19+
self.current = self.storage.head
1220

1321
def get(self):
1422
# Note: This is the only [] allowed
1523
list_buffer_contents = []
16-
24+
if len(self.storage) == 0:
25+
return list_buffer_contents
1726
# TODO: Your code here
27+
else:
28+
current_node = self.storage.head
29+
while current_node:
30+
if (current_node.value):
31+
list_buffer_contents.append(current_node.value)
32+
current_node = current_node.next
1833

1934
return list_buffer_contents
2035

@@ -23,10 +38,16 @@ def get(self):
2338

2439
class ArrayRingBuffer:
2540
def __init__(self, capacity):
26-
pass
41+
self.storage = [None]*capacity
42+
self.capacity = capacity
43+
self.current =0
2744

2845
def append(self, item):
29-
pass
46+
if (self.current == self.capacity):
47+
self.current = 0
48+
self.storage[self.current] = item
49+
self.current += 1
3050

3151
def get(self):
32-
pass
52+
53+
return [i for i in self.storage if i is not None]

0 commit comments

Comments
 (0)