There is a question like that:
1 X
2 X
3 X
4 X
5 X
6 X
7 X
8 X
9 = 1942
X
= must be x,+,-,÷ operators or nothing(89, 123 could be etc.)
How can i solve this problem with python?
Thanks.
There is a question like that:
1 X
2 X
3 X
4 X
5 X
6 X
7 X
8 X
9 = 1942
X
= must be x,+,-,÷ operators or nothing(89, 123 could be etc.)
How can i solve this problem with python?
Thanks.
You can start with something like this:
from itertools import product
target = 1942
test_str = "1{0[0]}2{0[1]}3{0[2]}4{0[3]}5{0[4]}6{0[5]}7{0[6]}8{0[7]}9"
for a in product(["*", "", "+", "/", "-", ""], repeat=8): # Iterate all posibilites
result_str = test_str.format(a)
if eval(result_str) == target:
print(result_str)
break
And optimize and make it more expandable to more numbers. But for your specific problem this works fine. I found this solution:
1*2/3+4*56*78/9
Take a look at eval
if you need more information.
You can use parser
module from python
import parser
formula = "1 + 2 + 3 + 4 + 5 * 6 * 7 * 8 * 9"
code = parser.expr(formula).compile()
print eval(code)