Skip to content
Open
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
Next Next commit
names.py - sub second fastest
  • Loading branch information
jchaing committed Aug 3, 2020
commit 957c22e0385942f0d3e81ce5d245499ab15097cf
27 changes: 21 additions & 6 deletions names/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,30 @@

duplicates = [] # Return the list of duplicates in this data structure

# Polynomial Runtime - O(n^2)
# Replace the nested for loops below with your improvements
for name_1 in names_1:
for name_2 in names_2:
if name_1 == name_2:
duplicates.append(name_1)
# for name_1 in names_1:
# for name_2 in names_2:
# if name_1 == name_2:
# duplicates.append(name_1)

# Linear Time O(n) - Runtime less than 2 seconds
for name in names_1:
if name in names_2:
duplicates.append(name)

# List Comprehension - Runtime less than 2 seconds
# duplicates = [name for name in names_1 if name in names_2]

# Set Functions to list - Runtime less than 1 second
# duplicates = list(set(names_1) & set(names_2))

# set().intersection() - Runtime less than 1 second
# duplicates = set(names_1).intersection(names_2)

end_time = time.time()
print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print (f"runtime: {end_time - start_time} seconds")
print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print(f"runtime: {end_time - start_time} seconds")

# ---------- Stretch Goal -----------
# Python has built-in tools that allow for a very efficient approach to this problem
Expand Down