Programmer disqualification problem 5 in Python if not solved in 1 hour (repost)

Code withdrawn due to personal reasons will be given for various reasons.

Theme: Five issues that will disqualify programmers if not solved within an hour

Python is amazing because it can solve problems that originally consider algorithms without thinking.

Please let me know how to solve it properly because I would like to refer to other people's articles!

What I'm doing is trying all combinations with ʻeval`, which is a system of pooping that dies if you neglect to check the arguments.

At first I thought it was an arithmetic operation!

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

How to solve other people

Recommended Posts

Programmer disqualification problem 5 in Python if not solved in 1 hour (repost)
Python / dictionary> setdefault ()> Add if not in dictionary
[Note] Project Euler in Python (Problem 1-22)
ABC166 in Python A ~ C problem
python> does not include the letters mm> if "mm" not in text: / print "not including mm"
Ant book in python: page 42 coin problem
Check if the URL exists in Python
Tkinter could not be imported in Python
Solve the maximum subarray problem in Python
What to do if you get "Python not configured." Using PyDev in Eclipse