Created
January 26, 2019 23:40
-
-
Save khayyamsaleem/59ab60f314a99f2f0d5dc8837af4e7f1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def pair_sum(my_list, target): | |
complements = {} | |
for i in range(len(my_list)): | |
rest = complements.get(my_list[i], None) | |
if rest: | |
return (rest[1], i) | |
complements[target - my_list[i]] = (my_list[i], i) | |
def pair_sum_slow(my_list, target): | |
for i in range(len(my_list)): | |
rest = target - my_list[i] | |
for j in range(i, len(my_list)): | |
if my_list[j] == rest: | |
return (i, j) | |
print(pair_sum([1,2,3,4,5],6)) | |
print(pair_sum([-1, -1, -2, -4, -5, -9, -12, -13],-21)) | |
print(pair_sum([1, -7, 2, 15, -11, 2], 42)) | |
print(pair_sum([0, -7, 2, 15, -11, 2], 2)) | |
print(pair_sum([1,1,2,1,1,1], 3)) | |
print("-"*10) | |
print(pair_sum_slow([1,2,3,4,5],6)) | |
print(pair_sum_slow([-1, -1, -2, -4, -5, -9, -12, -13],-21)) | |
print(pair_sum_slow([1, -7, 2, 15, -11, 2], 42)) | |
print(pair_sum_slow([0, -7, 2, 15, -11, 2], 2)) | |
print(pair_sum_slow([1,1,2,1,1,1], 3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment