Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.71 KB | None | 0 0
  1. def permutasi(prmts):
  2.  
  3.     if len(prmts) == 0:
  4.         return []
  5.     if len(prmts) == 1:
  6.         return [prmts]
  7.     l = []
  8.     for i in range(len(prmts)):
  9.        m = prmts[i]
  10.        remLst = prmts[:i] + prmts[i+1:]
  11.  
  12.        for p in permutasi(remLst):
  13.            l.append([m] + p)
  14.     return l
  15.  
  16. def operator(n):
  17.     global op
  18.     if (n ==0):
  19.         op = "+"
  20.     elif (n==1):
  21.         op = "-"
  22.     elif (n==2):
  23.         op = "*"
  24.     elif (n==3):
  25.         op ="/"
  26.  
  27. op = ""
  28. entry = ""
  29. answer = ""
  30. canswer = []
  31. inp = input().split(" ")
  32. for list_number in permutasi(inp):
  33.     number = ""
  34.     for o in list_number:
  35.         number += o
  36.     com_operator = ""
  37.     for i in range(4):
  38.         operator(i)
  39.         com_operator += op
  40.         for j in range(4):
  41.             operator(j)
  42.             com_operator += op
  43.             for k in range(4):
  44.                 operator(k)
  45.                 com_operator += op
  46.                 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]
  47.                 #print(eval(entry))
  48.                 answer = entry[0:2] + "(" + entry[2:5] + ")"+entry[5:7]
  49.                 try:
  50.                     result = eval(answer)
  51.                 except ZeroDivisionError:
  52.                     t = 0
  53.                 if (result == 24)and (not answer in canswer):
  54.                     canswer.append(answer)
  55.                 answer = "(" + entry[0:5] + ")" + entry[5:7]
  56.                 try:
  57.                     result = eval(answer)
  58.                 except ZeroDivisionError:
  59.                     t = 0
  60.                 if (result == 24)and (not answer in canswer):
  61.                     canswer.append(answer)
  62.                 answer = entry[0:2] + "(" + entry[2:7] + ")"
  63.                 try:
  64.                     result = eval(answer)
  65.                 except ZeroDivisionError:
  66.                     t = 0
  67.                 if (result == 24)and (not answer in canswer):
  68.                     canswer.append(answer)
  69.                 answer =  "(" + entry[0:2] + "(" + entry[2:5] + "))"+entry[5:7]
  70.                 try:
  71.                     result = eval(answer)
  72.                 except ZeroDivisionError:
  73.                     t = 0
  74.                 if (result == 24)and (not answer in canswer):
  75.                     canswer.append(answer)
  76.                 answer =  "((" + entry[0:3] + ")" + entry[3:5] + ")"+entry[5:7]
  77.                 try:
  78.                     result = eval(answer)
  79.                 except ZeroDivisionError:
  80.                     t = 0
  81.                 if (result == 24)and (not answer in canswer):
  82.                     canswer.append(answer)
  83.                 answer =  entry[0:2] + "(" + entry[2:4] + "(" + entry[4:7] + "))"
  84.                 try:
  85.                     result = eval(answer)
  86.                 except ZeroDivisionError:
  87.                     t = 0
  88.                 if (result == 24)and (not answer in canswer):
  89.                     canswer.append(answer)
  90.                 answer =  entry[0:2] + "((" + entry[2:5] + ")" + entry[5:7] + ")"
  91.                 try:
  92.                     result = eval(answer)
  93.                 except ZeroDivisionError:
  94.                     t = 0
  95.                 if (result == 24)and (not answer in canswer):
  96.                     canswer.append(answer)
  97.                 answer =  "(" + entry[0:3] + ")" + entry[3:4] + "(" + entry[4:7] + ")"
  98.                 try:
  99.                     result = eval(answer)
  100.                 except ZeroDivisionError:
  101.                     t = 0
  102.                 if (result == 24)and (not answer in canswer):
  103.                     canswer.append(answer)
  104.                 com_operator = com_operator[0:2]
  105.             com_operator = com_operator[0:1]
  106.         com_operator = ""
  107. print(canswer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement