|
14 | 14 |
|
15 | 15 | # Replace the nested for loops below with your improvements |
16 | 16 |
|
17 | | -# The runtime of the original code here was O(n^2) |
| 17 | +# --> The runtime of the original code here was O(n^2) <-- |
18 | 18 | # for name_1 in names_1: |
19 | 19 | # for name_2 in names_2: |
20 | 20 | # if name_1 == name_2: |
21 | 21 | # duplicates.append(name_1) |
22 | 22 |
|
23 | 23 | # The below imports assume that the given folders contain the requisite |
24 | 24 | # 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. |
25 | 47 | 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() |
36 | 52 | 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: |
40 | 56 | duplicates.append(name_2) |
41 | 57 |
|
| 58 | +# The runtime for this ended up being about 0.8 seconds on my computer-- |
| 59 | +# better than the Binary Search Tree! |
| 60 | + |
42 | 61 | end_time = time.time() |
43 | 62 | print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") |
44 | 63 | print (f"runtime: {end_time - start_time} seconds") |
|
0 commit comments