Skip to content

Instantly share code, notes, and snippets.

@zeroxia
Last active May 24, 2016 13:17
Show Gist options
  • Save zeroxia/a443a3defa6a2817b50b8bc74cbd526f to your computer and use it in GitHub Desktop.
Save zeroxia/a443a3defa6a2817b50b8bc74cbd526f to your computer and use it in GitHub Desktop.
For a list of numbers 1 2 3 4 5 6 7 8 9, add operators '+', '-', or none among them, to create an albegraic expression that evaluates to sum of 110.
#!/usr/bin/python
# vim: set ts=4 sts=4 et fileencoding=utf-8:
import itertools
target = 110
ops = [' + ', ' - ', '']
digit_list = list('123456789')
for op_list in itertools.product(ops, repeat=8):
expr = ''.join([ item for pair in zip(digit_list, op_list + ('',)) for item in pair ])
try:
if eval(expr) == target: print "%s = %d" % (expr, target)
except: pass
## From CCF.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment