Skip to content

Instantly share code, notes, and snippets.

@o-az
Last active July 21, 2021 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save o-az/bdf6a3027d75015fc4d28c8cec5c6c83 to your computer and use it in GitHub Desktop.
Save o-az/bdf6a3027d75015fc4d28c8cec5c6c83 to your computer and use it in GitHub Desktop.
Two TwoSum implementations
# x + y = target
def twoSum(array, targetSum):
seen = {}
for index, x in enumerate(array):
y = targetSum - x
if y in seen:
return [x, y]
else:
seen[x] = index
return []
# worse
def twoNumberSum_2(array, targetSum):
for (index_1, item_1) in enumerate(array):
for (_, item_2) in enumerate(array[index_1 + 1:]):
if item_1 + item_2 == targetSum:
return [item_1, item_2]
return []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment