Skip to content

Instantly share code, notes, and snippets.

@sreevidyavutukuru
Created July 13, 2017 23:52
Show Gist options
  • Save sreevidyavutukuru/ab15d8b4265b9e059e988a0bd7a2907d to your computer and use it in GitHub Desktop.
Save sreevidyavutukuru/ab15d8b4265b9e059e988a0bd7a2907d to your computer and use it in GitHub Desktop.
list1 = [1,2,2,3,4,5,6,9]
def pair_sums(nums,n):
dict_nums = {}
result = []
nums.sort()
for val in nums:
if dict_nums.has_key(val):
dict_nums[val] += 1
else:
dict_nums[val] = 1
#print dict_nums
for val in dict_nums:
diff = n-val
#print diff
neededVal = diff
if dict_nums.has_key(neededVal) and (neededVal!= val or dict_nums[neededVal]>1) and val<=neededVal:
result.append([val,neededVal])
return result
print pair_sums(list1,4)
'''and (dict_nums[neededVal]!= neededVal or dict_nums[neededVal]>1)'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment