Skip to content

Instantly share code, notes, and snippets.

@trthatcher
Forked from winniethemu/subset_sum.py
Last active August 29, 2015 14:13
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 trthatcher/21418e2eb0dbd64922e4 to your computer and use it in GitHub Desktop.
Save trthatcher/21418e2eb0dbd64922e4 to your computer and use it in GitHub Desktop.
1
2
3
4
6
3
2
5
76
567
23
54
4234
import sys
list_of_integers = []
for string_value in open('list_of_integers', 'r').read().splitlines():
list_of_integers.append(int(string_value))
def populate_dictionary(dictionary, list_of_integers):
for integer in list_of_integers:
dictionary[integer] = True
def search_array(dictionary, list_of_integers, sum_value):
for integer in list_of_integers:
if dictionary.get(sum_value - integer, False):
return True
return False
def subset_sum2(list_of_integers, sum_value):
dictionary = {}
populate_dictionary(dictionary, list_of_integers)
return search_array(dictionary, list_of_integers, sum_value)
for string_value in sys.argv[1:]:
print subset_sum2(list_of_integers, int(string_value))
@winniethemu
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment