-
-
Save dudarev/b5fb033de085623d38fb to your computer and use it in GitHub Desktop.
Target sum problem
This file contains 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
``` | |
231552 - yes | |
234756 - no | |
596873 - yes | |
648219 - yes | |
726312 - yes | |
981237 - no | |
988331 - yes | |
1277361 - no | |
1283379 - no | |
``` | |
This file contains 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
""" | |
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