Skip to content

Instantly share code, notes, and snippets.

@itsjohncs
Created February 19, 2020 02:16
Show Gist options
  • Save itsjohncs/c2d3eb4ba3457ef7249211989dd92598 to your computer and use it in GitHub Desktop.
Save itsjohncs/c2d3eb4ba3457ef7249211989dd92598 to your computer and use it in GitHub Desktop.
#!/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