Created
February 19, 2020 02:16
-
-
Save itsjohncs/c2d3eb4ba3457ef7249211989dd92598 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
#!/usr/bin/env python3 | |
import sys | |
def doesAny2SumTo(target, lst): | |
multiset = {} | |
for i in lst: | |
if i in multiset: | |
multiset[i] += 1 | |
else: | |
multiset[i] = 1 | |
for i in lst: | |
# Try to find the counterpart number where i + counterpart == target | |
counterpart = target - i | |
count = multiset.get(counterpart, 0) | |
if ((counterpart == i and count >= 2) or | |
(counterpart != i and count >= 1)): | |
return True | |
return False | |
if __name__ == "__main__": | |
if len(sys.argv) < 3: | |
print(f"{sys.argv[0]} TARGET NUM [NUM ...]", file=sys.stderr) | |
print(doesAny2SumTo(int(sys.argv[1]), [int(i) for i in sys.argv[2:]])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment