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
Recommended Posts