Skip to content

Instantly share code, notes, and snippets.

@vanzaj
Created June 22, 2019 08:52
Show Gist options
  • Save vanzaj/ad4bdfcfe52be8ee1b40e53e468fe0d1 to your computer and use it in GitHub Desktop.
Save vanzaj/ad4bdfcfe52be8ee1b40e53e468fe0d1 to your computer and use it in GitHub Desktop.
sum to 100
#!/usr/local/bin/python3
from itertools import chain, product
flatten = chain.from_iterable
digits = [ str(x+1) for x in range(9) ]
P, M, C = [ ' + ', ' - ', '']
''' operators = add, sub, concat (str, str)
1 + 2 + 3 + ...
1 - 2 - 3 + ...
1 - 23 + 4 + ...
'''
def foo(digits, ops):
return ''.join((flatten(zip(digits, ops)) )) + digits[-1]
assert foo(digits[:4], [P,P,P] ) == '1 + 2 + 3 + 4'
#print(list(product('ab', repeat=3)))
for p in product([P,M,C], repeat=8):
f = foo(digits, p)
if eval(f) == 100:
print(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment