Skip to content

Instantly share code, notes, and snippets.

@snahor
Forked from ukd1/readme.md
Last active August 29, 2015 14:15
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 snahor/f3cc9a40110a7ad6aa0b to your computer and use it in GitHub Desktop.
Save snahor/f3cc9a40110a7ad6aa0b to your computer and use it in GitHub Desktop.

Given an array of positive integers, write a function which returns all the unique pairs which add (equal) up to 100.

Example data:

sample_data = [0, 1, 100, 99, 0, 10, 90, 30, 55, 33, 55, 75, 50, 51, 49, 50, 51, 49, 51]
sample_output = [[1,99], [0,100], [10,90], [51,49], [50,50]]
def f(lst):
xs = sorted(lst)
ps = []
i = 0
j = len(lst) - 1
while i < j:
if xs[i] + xs[j] == 100:
ps.append((xs[i], xs[j]))
i += 1
j -= 1
elif xs[i] + xs[j] > 100:
j -= 1
elif xs[i] + xs[j] < 100:
i += 1
return set(ps)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment