3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

1時間で解けなかったらプログラマ失格問題5をPythonで(再掲)

Last updated at Posted at 2015-06-01

一身上の都合により取り下げたコードを諸事情でもっかいあげます。

お題:1時間以内に解けなければプログラマ失格となってしまう5つの問題が話題に

本来アルゴリズムを考える問題でも考えずに解けてしまうPythonはすごい。

まっとうな解き方は他の方の記事を参考にしたいので教えてください!

やってる事は全組み合わせをevalで試すという、引数のチェックを怠ると死ぬ系うんこ処理で実現させました。

最初は四則演算かと思ってたよ!

q5.py
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

他の方々の解き方

3
2
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?