Created
June 30, 2016 20:48
-
-
Save danielrmeyer/953cec4efb1d5a69d019bf3f9b930b00 to your computer and use it in GitHub Desktop.
find all pairs in a list that add to a target number
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 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