Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def permutasi(prmts):
- if len(prmts) == 0:
- return []
- if len(prmts) == 1:
- return [prmts]
- l = []
- for i in range(len(prmts)):
- m = prmts[i]
- remLst = prmts[:i] + prmts[i+1:]
- for p in permutasi(remLst):
- l.append([m] + p)
- return l
- def operator(n):
- global op
- if (n ==0):
- op = "+"
- elif (n==1):
- op = "-"
- elif (n==2):
- op = "*"
- elif (n==3):
- op ="/"
- op = ""
- entry = ""
- answer = ""
- canswer = []
- inp = input().split(" ")
- for list_number in permutasi(inp):
- number = ""
- for o in list_number:
- number += o
- com_operator = ""
- for i in range(4):
- operator(i)
- com_operator += op
- for j in range(4):
- operator(j)
- com_operator += op
- for k in range(4):
- operator(k)
- com_operator += op
- entry = number[0:1] + com_operator[0:1] + number[1:2] + com_operator[1:2] + number[2:3] + com_operator[2:3] + number[3:4]
- #print(eval(entry))
- answer = entry[0:2] + "(" + entry[2:5] + ")"+entry[5:7]
- try:
- result = eval(answer)
- except ZeroDivisionError:
- t = 0
- if (result == 24)and (not answer in canswer):
- canswer.append(answer)
- answer = "(" + entry[0:5] + ")" + entry[5:7]
- try:
- result = eval(answer)
- except ZeroDivisionError:
- t = 0
- if (result == 24)and (not answer in canswer):
- canswer.append(answer)
- answer = entry[0:2] + "(" + entry[2:7] + ")"
- try:
- result = eval(answer)
- except ZeroDivisionError:
- t = 0
- if (result == 24)and (not answer in canswer):
- canswer.append(answer)
- answer = "(" + entry[0:2] + "(" + entry[2:5] + "))"+entry[5:7]
- try:
- result = eval(answer)
- except ZeroDivisionError:
- t = 0
- if (result == 24)and (not answer in canswer):
- canswer.append(answer)
- answer = "((" + entry[0:3] + ")" + entry[3:5] + ")"+entry[5:7]
- try:
- result = eval(answer)
- except ZeroDivisionError:
- t = 0
- if (result == 24)and (not answer in canswer):
- canswer.append(answer)
- answer = entry[0:2] + "(" + entry[2:4] + "(" + entry[4:7] + "))"
- try:
- result = eval(answer)
- except ZeroDivisionError:
- t = 0
- if (result == 24)and (not answer in canswer):
- canswer.append(answer)
- answer = entry[0:2] + "((" + entry[2:5] + ")" + entry[5:7] + ")"
- try:
- result = eval(answer)
- except ZeroDivisionError:
- t = 0
- if (result == 24)and (not answer in canswer):
- canswer.append(answer)
- answer = "(" + entry[0:3] + ")" + entry[3:4] + "(" + entry[4:7] + ")"
- try:
- result = eval(answer)
- except ZeroDivisionError:
- t = 0
- if (result == 24)and (not answer in canswer):
- canswer.append(answer)
- com_operator = com_operator[0:2]
- com_operator = com_operator[0:1]
- com_operator = ""
- print(canswer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement