Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adds solution for names
  • Loading branch information
ethyl2 committed Apr 24, 2020
commit fe902b8cd8c1f03ca42c499eb688a3e0dada7ee4
30 changes: 26 additions & 4 deletions names/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,27 @@

start_time = time.time()

bst = BinarySearchTree('April')
bst = BinarySearchTree('Heather')
# f = open('names_1.txt', 'r')
with open(os.path.join(sys.path[0], 'names_1.txt'), 'r') as f:
names_1 = f.read().split("\n") # List containing 10000 names
# names_1.insert(f.read().split("\n"))
# f.close()

# print(names_1[0])
# print('Jean Velazquez' < 'MandyLee')
bst.insert(names_1[0])
# print('Jean Velazquez' < 'Heather')
# bst.insert(names_1[0])
# print(bst.get_max())
# print(bst.contains('Jean Velazquez'))
'''
for name in names_1:
bst.insert(name)
'''

# print(bst.get_max())
# print(bst.contains('Jean Velazquez'))
print(bst.contains('April'))
# print(bst.contains('Heather'))
# print(bst.contains('Zoie Lloyd'))

# f = open('names_2.txt', 'r')
with open(os.path.join(sys.path[0], 'names_2.txt'), 'r') as f:
Expand All @@ -33,6 +35,26 @@
duplicates = [] # Return the list of duplicates in this data structure

# Replace the nested for loops below with your improvements


def binary_search(arr, target):
low = 0
high = len(arr) - 1
while low <= high:
middle = (low + high)//2
if target == arr[middle]:
return middle
elif target < arr[middle]:
high = middle - 1
elif target > arr[middle]:
low = middle + 1
return -1


names_1.sort()
for name in names_2:
if binary_search(names_1, name) != -1:
duplicates.append(name)
'''
for name_1 in names_1:
for name_2 in names_2:
Expand Down