Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. """
  2. X Algorithm testing
  3.  
  4. @author Mateusz Warzyński
  5. @license WTFPL
  6.  
  7. @python version tested: 3.4
  8.  
  9. Usage: python3.4 x_algorithm_testing.py "x.execution.file"
  10. where x.execution.file is path to your compiled code
  11. """
  12.  
  13.  
  14.  
  15. import subprocess
  16. import random
  17. import string
  18. import sys
  19.  
  20.  
  21. class XAlgorithm:
  22.  
  23. data = []
  24. lines = []
  25.  
  26. solutions = []
  27.  
  28. def load_data(self, data):
  29. self.data = data
  30.  
  31. def add_line(self, table, line):
  32. add = 1
  33.  
  34. for letter in table:
  35. if len(letter) > 0 and letter not in table:
  36. add = 0
  37. break
  38.  
  39. if add:
  40. for x in range(0, len(self.data[line])):
  41. if len(self.data[line][x]) > 0:
  42. table[x] = self.data[line][x]
  43.  
  44. def delete_line(self, table, line):
  45.  
  46. for x in range(0, len(self.data[line])):
  47. if table[x] == self.data[line][x]:
  48. table[x] = ''
  49.  
  50. def solve(self, current_table, start_iterate=0):
  51.  
  52. solution = len(self.data[0])
  53. counter = 0
  54.  
  55. forward = 1
  56.  
  57. for x in range(start_iterate, len(self.data)):
  58.  
  59. for y in range(0, len(self.data[x])):
  60.  
  61. if len(current_table[y]) > 0 and len(self.data[x][y]) > 0:
  62. forward = 0
  63. break
  64.  
  65. if forward:
  66.  
  67. self.add_line(current_table, x)
  68.  
  69. for letter in current_table:
  70. if len(letter):
  71. counter += 1
  72.  
  73. if counter == solution:
  74. self.solutions.append("".join(current_table))
  75.  
  76. else:
  77. if counter < solution:
  78. self.solve(current_table, x)
  79.  
  80. self.delete_line(current_table, x)
  81.  
  82. forward = 1
  83. counter = 0
  84.  
  85. def find(self):
  86. del self.solutions[:]
  87.  
  88. self.solve(['']*len(self.data[0]))
  89. return self.solutions
  90.  
  91.  
  92. class StartTheRocket:
  93.  
  94. data = []
  95.  
  96. xAlgorithm = None
  97.  
  98. def __init__(self):
  99. self.xAlgorithm = XAlgorithm()
  100.  
  101. def random_word(self, length):
  102.  
  103. string = ""
  104.  
  105. count_spaces = 0
  106. count = 0
  107.  
  108. for x in range(0, length):
  109.  
  110. if random.randint(0, 1):
  111. string += " "
  112. else:
  113. string += random.choice('qwertyuiopasdfghjklzxcvbnm')
  114.  
  115. count += 1
  116.  
  117. if count == length:
  118. break
  119.  
  120. return string
  121.  
  122. def prepare_data(self):
  123. string = ""
  124.  
  125. for item in self.data:
  126. for letter in item:
  127. if len(letter) > 0:
  128. string += letter
  129. else:
  130. string += " "
  131. string += "\n"
  132.  
  133. return string
  134.  
  135. def execute(self):
  136. command = 'echo "'+str(self.prepare_data())+'" | ./'+str(sys.argv[1])
  137.  
  138. output = subprocess.check_output(command, shell=True)
  139. output = str(output)[2:].split("\\n")
  140.  
  141. result_data = []
  142.  
  143. for result in output:
  144. if len(result) == len(self.data[0]):
  145. result_data.append(result)
  146.  
  147. return result_data
  148.  
  149. def rocket_check(self, data):
  150. self.data = data
  151. self.xAlgorithm.load_data(data)
  152.  
  153. original_data = self.execute()
  154. check_data = self.xAlgorithm.find()
  155.  
  156. for result in original_data:
  157. if result not in check_data:
  158. printf("Error! No '"+str(result)+'" in generated checked data.')
  159. return 1
  160.  
  161. return 0
  162.  
  163. def milky_way(self):
  164.  
  165. x = 10
  166.  
  167. while True:
  168.  
  169. data = []
  170.  
  171. for y in range(0, x):
  172. data.append(self.random_word(x))
  173.  
  174. print("TEST "+str(x)+":")
  175.  
  176. if not self.rocket_check(data):
  177. print("OK!\n")
  178.  
  179. x += 1
  180.  
  181. rocket = StartTheRocket()
  182. rocket.milky_way()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement