Skip to content

Commit aaa06f7

Browse files
committed
finished stretch goal in names.py
1 parent 86bb54c commit aaa06f7

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

names/names.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,50 @@
1414

1515
# Replace the nested for loops below with your improvements
1616

17-
# The runtime of the original code here was O(n^2)
17+
# --> The runtime of the original code here was O(n^2) <--
1818
# for name_1 in names_1:
1919
# for name_2 in names_2:
2020
# if name_1 == name_2:
2121
# duplicates.append(name_1)
2222

2323
# The below imports assume that the given folders contain the requisite
2424
# data structures.
25+
# import sys
26+
# sys.path.append('../../Data-Structures/binary_search_tree')
27+
# sys.path.append('../../Data-Structures/queue_and_stack')
28+
# sys.path.append('../../Data-Structures/doubly_linked_list')
29+
# from binary_search_tree import BinarySearchTree
30+
31+
# # This has a runtime complexity of O(n log n), I think.
32+
# # It took about 0.1 seconds to run on my computer.
33+
# tree = BinarySearchTree(names_1[0])
34+
# for name_1 in names_1[1:]:
35+
# tree.insert(name_1)
36+
# for name_2 in names_2:
37+
# # The "contains" method checks if the value exists in the tree.
38+
# # On average, its runtime is O(log n).
39+
# if tree.contains(name_2):
40+
# duplicates.append(name_2)
41+
42+
# STRETCH SOLUTION
43+
# Basically I want to search names_1 for each name_2, and add to
44+
# "duplicates" if I find it.
45+
# The most efficient search with arrays is a Binary Search,
46+
# so I'll import my code for that.
2547
import sys
26-
sys.path.append('../../Data-Structures/binary_search_tree')
27-
sys.path.append('../../Data-Structures/queue_and_stack')
28-
sys.path.append('../../Data-Structures/doubly_linked_list')
29-
from binary_search_tree import BinarySearchTree
30-
31-
# This has a runtime complexity of O(n log n), I think.
32-
# It took about 0.1 seconds to run on my computer.
33-
tree = BinarySearchTree(names_1[0])
34-
for name_1 in names_1[1:]:
35-
tree.insert(name_1)
48+
sys.path.append('../../Sorting/src/searching')
49+
from searching import binary_search
50+
# To do a binary search, you first have to sort the search array.
51+
names_1.sort()
3652
for name_2 in names_2:
37-
# The "contains" method checks if the value exists in the tree.
38-
# On average, its runtime is O(log n).
39-
if tree.contains(name_2):
53+
# binary_search returns a -1 when the item isn't found,
54+
# so append to duplicates whenever the result isn't -1.
55+
if binary_search(names_1, name_2) != -1:
4056
duplicates.append(name_2)
4157

58+
# The runtime for this ended up being about 0.8 seconds on my computer--
59+
# better than the Binary Search Tree!
60+
4261
end_time = time.time()
4362
print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
4463
print (f"runtime: {end_time - start_time} seconds")

0 commit comments

Comments
 (0)