Skip to content
Merged
Show file tree
Hide file tree
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
Fixes contains() in bst to work with strings, and adds option of usin…
…g bst for names.py
  • Loading branch information
ethyl2 committed Apr 24, 2020
commit 2ec6fcc41ffbccf12708ca218d8a2b8356265e4a
4 changes: 3 additions & 1 deletion names/binary_search_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ def contains(self, target):
# Return True if the tree contains the value
# False if it does not
while self:
if target is self.value:
# print(self.value)
# print(target)
if target == self.value:
return True
elif target < self.value:
if not self.left:
Expand Down
29 changes: 11 additions & 18 deletions names/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,21 @@

start_time = time.time()

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' < 'Heather')
# bst.insert(names_1[0])
# print(bst.get_max())
# print(bst.contains('Jean Velazquez'))
# Uncomment below to use the bst:
'''
bst = BinarySearchTree('Heather')
for name in names_1:
bst.insert(name)
'''

# print(bst.get_max())
# print(bst.contains('Jean Velazquez'))
# 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:
names_2 = f.read().split("\n") # List containing 10000 names
# f.close()

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
Expand All @@ -51,16 +35,25 @@ def binary_search(arr, target):
return -1


# This implementation doesn't use the bst
# 0.08800506591796875 seconds
names_1.sort()
for name in names_2:
if binary_search(names_1, name) != -1:
duplicates.append(name)


# This is the original implementation:
# About 10 seconds
'''
for name_1 in names_1:
for name_2 in names_2:
if name_1 == name_2:
duplicates.append(name_1)
'''

# This implementation uses the bst:
# 0.13100862503051758 seconds
'''
for name in names_2:
if bst.contains(name):
Expand Down