Skip to content

Instantly share code, notes, and snippets.

@tAkihiko
Last active August 29, 2015 14:22
Show Gist options
  • Save tAkihiko/44abe86535f8d21bb73d to your computer and use it in GitHub Desktop.
Save tAkihiko/44abe86535f8d21bb73d to your computer and use it in GitHub Desktop.
1時間で解けなかったらプログラマ失格問題5をPythonで(再掲) ref: http://qiita.com/tAkihiko/items/13e78befd63ff0bf001d
import itertools as it
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]
num_str = map(str, nums)
#ops = ["+", "-", "/", "*", ""]
ops = [" + ", " - ", ""]
opss = it.tee(ops, len(nums) - 1)
for op_c in it.product(*opss):
evl = num_str[0]
for idx, op in enumerate(op_c):
evl += op + num_str[idx+1]
rst = eval(evl)
if rst == 100:
print evl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment