Skip to content

Instantly share code, notes, and snippets.

@dudarev
Forked from dmitriy-kiriyenko/answers.txt
Created June 18, 2012 19:49
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 dudarev/b5fb033de085623d38fb to your computer and use it in GitHub Desktop.
Save dudarev/b5fb033de085623d38fb to your computer and use it in GitHub Desktop.
Target sum problem
```
231552 - yes
234756 - no
596873 - yes
648219 - yes
726312 - yes
981237 - no
988331 - yes
1277361 - no
1283379 - no
```
"""
Usage:
$ python target_sum.py 231552
yes
"""
import sys
target = int(sys.argv[1])
# http://stackoverflow.com/questions/2701173/most-efficient-way-for-a-lookup-search-in-a-huge-list-python
# http://wiki.python.org/moin/TimeComplexity
# if memory usage is critical one should use sorted list with bisect from standard library instead of a set
pairs = set()
for i in open('input.txt'):
n = int(i)
if (target - n) in pairs:
print 'yes'
sys.exit(0)
pairs.add(n)
print 'no'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment