Skip to content

Commit 02d5eff

Browse files
committed
First try with names.py
I am getting a little stumped on this one. I am pretty sure this is O(log n), but the time complexity is 32 seconds. I tried to check the names as they were being inserted to make it faster, but that fails because their is duplicates inside their own names file. Need to keep thinking about it.
1 parent 76c34ff commit 02d5eff

File tree

1 file changed

+70
-7
lines changed

1 file changed

+70
-7
lines changed

names/names.py

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import time
22

3+
34
start_time = time.time()
45

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

14+
# duplicates = []
15+
# for name_1 in names_1:
16+
# for name_2 in names_2:
17+
# if name_1 == name_2:
18+
# duplicates.append(name_1)
19+
20+
# end_time = time.time()
21+
# print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
22+
# print(f"runtime: {end_time - start_time} seconds")
23+
24+
25+
class BinarySearchTree:
26+
def __init__(self, value):
27+
self.value = value
28+
self.left = None
29+
self.right = None
30+
self.duplicates = []
31+
32+
def insert(self, value):
33+
new_tree = BinarySearchTree(value)
34+
if (value < self.value):
35+
if not self.left:
36+
self.left = new_tree
37+
else:
38+
self.left.insert(value)
39+
elif value >= self.value:
40+
if not self.right:
41+
self.right = new_tree
42+
else:
43+
self.right.insert(value)
44+
45+
def contains(self, target):
46+
if self.value == target:
47+
return True
48+
if self.left:
49+
if self.left.contains(target):
50+
return True
51+
if self.right:
52+
if self.right.contains(target):
53+
return True
54+
return False
55+
56+
def get_max(self):
57+
if not self:
58+
return None
59+
max_value = self.value
60+
current = self
61+
while current:
62+
if current.value > max_value:
63+
max_value = current.value
64+
current = current.right
65+
return max_value
66+
67+
1368
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)
1869

19-
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")
70+
firstName = names_1.pop(0)
71+
72+
bst = BinarySearchTree(firstName)
73+
for name in names_1:
74+
bst.insert(name)
75+
76+
for name in names_2:
77+
if bst.contains(name):
78+
duplicates.append(name)
2279

80+
81+
print(bst.duplicates)
82+
83+
end_time = time.time()
84+
print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
85+
print(f"runtime: {end_time - start_time} seconds")

0 commit comments

Comments
 (0)