Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. import inspect
  2. import ast
  3. import itertools
  4. import sys
  5. import os
  6. import socket
  7. from random import shuffle
  8.  
  9. def isFloat(s):
  10. try:
  11. float(s)
  12. except ValueError:
  13. return False
  14. return True
  15.  
  16. def checkCalc(s):
  17. try:
  18. e = eval(s)
  19. except ZeroDivisionError:
  20. return False
  21. except SyntaxError:
  22. return False
  23. return e
  24.  
  25. def adding_a_parenthese(arr):
  26. result = []
  27. for i in range(0, len(arr) - 1):
  28. tmp = []
  29. for j in range(0, i):
  30. tmp.append(arr[j])
  31. tmp.append('(')
  32. tmp.append(arr[i])
  33. tmp.append(arr[i + 1])
  34. tmp.append(')')
  35. tmp.extend(arr[i+2:])
  36. result.append(tmp)
  37. return result
  38.  
  39. def concentrate(head, tail):
  40. res = []
  41. for i in head:
  42. res.append(i)
  43. for i in tail:
  44. res.append(i)
  45. return res
  46.  
  47. def cut_down_parenthese_pair(arr):
  48. head = []
  49. for i in range(0, len(arr)):
  50. head.append(arr[i])
  51. if(arr[i] == ')'):
  52. return (head, arr[i+1:])
  53.  
  54. def adding_operation(arr, operation_str):
  55. res = []
  56. j = 0
  57. for i in range(0, len(arr)):
  58. res.append(arr[i])
  59. if(isFloat(arr[i]) == True and i != len(arr) - 1):
  60. if(arr[i + 1] != ")" ):
  61. res.append(operation_str[j])
  62. j += 1
  63. else:
  64. if(arr[i] == ')' and i != len(arr) - 1):
  65. res.append(operation_str[j])
  66. j += 1
  67. return res
  68.  
  69. def int_to_float(arr):
  70. for i in range(0, len(arr)):
  71. if(isFloat(arr[i]) == True):
  72. arr[i] = str(float(arr[i]))
  73. return arr
  74.  
  75. def expanse_parenthese(arr):
  76. pos = arr.index('(')
  77. if(pos - 2 > 0 and pos + 4 < len(arr)):
  78. arr.insert(pos - 2, '(')
  79. arr.insert(pos + 5, ')')
  80. else:
  81. return False
  82. return arr
  83.  
  84. #Extract from input
  85. st = "4 52 79 1 55 7 63 7 3.3399539700805523"
  86. s = st.split()
  87. chars = "+-*/"
  88. op_str_arr = []
  89. expect = float(s[len(s) - 1])
  90. s.pop()
  91. count = len(s) - 1
  92.  
  93. #generate operator list
  94. for item in itertools.product(chars, repeat=count):
  95. op_str_arr.append("".join(item))
  96. # print op_str_arr
  97. #generate parenthese list
  98. all_possible = adding_a_parenthese(s)
  99. for c in adding_a_parenthese(s):
  100. head, tail = cut_down_parenthese_pair(c)
  101. for t in adding_a_parenthese(tail):
  102. res = concentrate(head, t)
  103. all_possible.append(res)
  104. head_2, tail_2 = cut_down_parenthese_pair(t)
  105. for n in adding_a_parenthese(tail_2):
  106. res = concentrate(head_2, n)
  107. res = concentrate(head, res)
  108. all_possible.append(res)
  109.  
  110. # print len(all_possible)
  111. all_possible_op = []
  112.  
  113. for op_str in op_str_arr:
  114. for possible in all_possible:
  115. all_possible_op.append(adding_operation(possible, op_str))
  116. all_possible_op.reverse()
  117. for test in all_possible_op:
  118. expanse = expanse_parenthese(test)
  119. if(expanse != False):
  120. all_possible_op.append(expanse)
  121.  
  122. calculated = 0
  123. # test = ["1", "+", "2", "/", "(","4", "+", "3", ")", "+", "(", "9", "+", "1", ")", "-", "5"]
  124. # # print all_possible_op.index(test)
  125. # print eval("".join(int_to_float(all_possible_op[53218])))
  126. # f = open("res.txt", "wb")
  127. max_possible = len(all_possible_op)
  128. print max_possible
  129. for pos_op in all_possible_op:
  130. cl = ("".join(int_to_float(pos_op)))
  131. checkcalc = checkCalc(cl)
  132. if(checkcalc != False):
  133. res = checkcalc
  134. else:
  135. res = 0
  136. # calculated += 1
  137. # if calculated == 53218:
  138. # print cl
  139. # print expect
  140. # print res
  141. # print type(expect)
  142. # print type(res)
  143. #f.write(cl + "\n")
  144. #f.write(str(float(res)) + "\n")
  145. if str(res) == str(expect):
  146. print "Found"
  147. print cl
  148. print res
  149. sys.exit()
  150. print "Done"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement