I want to check if 7 digits can get to 100 when putting elementary arithmetic symbols between them.
def is_hundred(n1,n2,n3,n4,n5,n6,n7):
p = [+,-,*,/]
for p1 in p:
for p2 in p:
for p3 in p:
for p4 in p:
for p5 in p:
for p6 in p:
if n1 p1 n2 p2 n3 p3 n4 p4 n5 p5 n6 p6 n7 == 100:
return "success"
how can i replace the variables with the arithmetic symbols that are in the list?
In case you didn't hear it: using
evalis evil. So that's definitely not code that I'd put in production. That said, this exercise is also something that I will probably never have to deploy to prod...The idea: use the arguments and "brute-force" all the possibilities of adding operations between them. Construct these options as strings, and for each one of them, when done, evaluate the total (using
eval) and if the total is 100 - print it to stdout: