Skip to content

Commit 1c612e1

Browse files
committed
optimized names to about a third of a second
1 parent ac5648d commit 1c612e1

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

names/names.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,45 +34,49 @@ def insert(self, value):
3434
def contains(self, target):
3535
# compare target to root
3636
if target == self.value:
37-
print(f"target {target} == self.value {self.value}")
3837
return True
3938
# if it's smaller, check left side
4039
elif target < self.value:
41-
print(f"target {target} < self.value {self.value}")
4240
# first check that there is a left side; if not, return False
4341
if not self.left:
44-
print("no self.left")
4542
return False
4643
# else, if it doesn't match the left side, call contains on left side with same target
4744
elif target != self.left:
48-
print(f"target {target} != self.left {self.left}")
4945
return self.left.contains(target)
5046
else:
51-
print(f"target {target} == self.left {self.left}")
5247
return True
5348
# else, check right side
5449
elif target > self.value:
55-
print(f"target {target} > self.value {self.value}")
5650
# first check that there is a right side; if not, return False
5751
if not self.right:
58-
print("no self.right")
5952
return False
6053
# else, if it doesn't match the right side, call contains on right side with same target
6154
elif target != self.right:
62-
print(f"target {target} != self.right {self.right}")
6355
return self.right.contains(target)
6456
else:
65-
print(f"target {target} == self.right {self.right}")
6657
return True
6758
pass
6859

69-
# 20.11 seconds
60+
7061
duplicates = []
62+
63+
# ~20 seconds
7164
# for name_1 in names_1:
7265
# for name_2 in names_2:
7366
# if name_1 == name_2:
7467
# duplicates.append(name_1)
7568

69+
# ~0.33 seconds!!
70+
# plug in first value to root
71+
tree = BinarySearchTree(names_1[0])
72+
# add first file's names to tree
73+
for name in names_1:
74+
tree.insert(name)
75+
for name in names_2:
76+
# use contain search function to find duplicates
77+
if tree.contains(name):
78+
# add duplicates to list
79+
duplicates.append(name)
7680

7781

7882
end_time = time.time()

0 commit comments

Comments
 (0)