Created
February 17, 2016 05:05
-
-
Save alvintamie/615a012ad3e1c67672d3 to your computer and use it in GitHub Desktop.
31.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
t = 31 | |
cl = [10,9,8,4] | |
operations = ["+", "-", "*", "/"] | |
def r(a,b,o): | |
a = float(a) | |
b = float(b) | |
if o == "*": | |
return a*b | |
elif o == "-": | |
return a-b | |
elif o == "+": | |
return a+b | |
elif o == "/": | |
if b == 0: | |
return -1000 | |
return a/b | |
for c in itertools.permutations(cl, 4): | |
for o1 in operations: | |
for o2 in operations: | |
for o3 in operations: | |
# single bracket | |
b = c[0] | |
b = r(b, c[1], o1) | |
b = r(b, c[2], o2) | |
b = r(b, c[3], o3) | |
if b == t: | |
print str(c[0]) + o1 + str(c[1]) + o2 + str(c[2]) + o3 + str(c[3]) | |
# 2 bracket | |
b1 = r(c[0],c[1], o1) | |
b2 = r(c[2],c[3], o3) | |
b = r(b1,b2,o2) | |
if b == t: | |
print "(" + str(c[0]) + o1 + str(c[1]) + ")" + o2 + "(" + str(c[2]) + o3 + str(c[3]) + ")" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment