Skip to content

Instantly share code, notes, and snippets.

@tAkihiko
Last active August 29, 2015 14:21
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 tAkihiko/446df40339688921629d to your computer and use it in GitHub Desktop.
Save tAkihiko/446df40339688921629d to your computer and use it in GitHub Desktop.
1時間で解けなかったらプログラマ失格問題5をPythonで ref: http://qiita.com/tAkihiko/items/07ede10063ef1c5d7415
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