Skip to content

Instantly share code, notes, and snippets.

@danielrmeyer
Created June 30, 2016 20:48
Show Gist options
  • Save danielrmeyer/953cec4efb1d5a69d019bf3f9b930b00 to your computer and use it in GitHub Desktop.
Save danielrmeyer/953cec4efb1d5a69d019bf3f9b930b00 to your computer and use it in GitHub Desktop.
find all pairs in a list that add to a target number
def sum2target(l, t):
"""
:param l: list of positive itegers
:param t: a target number
:return: set of pairs of numbers in l that sum to t
"""
d = {t-x: x for x in l}
return {frozenset((x, d[x])) for x in l if x in d.keys()}
if __name__ == '__main__':
print(sum2target((3, 4, 10, 1, 6), 7))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment