Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- X Algorithm testing
- @author Mateusz Warzyński
- @license WTFPL
- @python version tested: 3.4
- Usage: python3.4 x_algorithm_testing.py "x.execution.file"
- where x.execution.file is path to your compiled code
- """
- import subprocess
- import random
- import string
- import sys
- class XAlgorithm:
- data = []
- lines = []
- solutions = []
- def load_data(self, data):
- self.data = data
- def add_line(self, table, line):
- add = 1
- for letter in table:
- if len(letter) > 0 and letter not in table:
- add = 0
- break
- if add:
- for x in range(0, len(self.data[line])):
- if len(self.data[line][x]) > 0:
- table[x] = self.data[line][x]
- def delete_line(self, table, line):
- for x in range(0, len(self.data[line])):
- if table[x] == self.data[line][x]:
- table[x] = ''
- def solve(self, current_table, start_iterate=0):
- solution = len(self.data[0])
- counter = 0
- forward = 1
- for x in range(start_iterate, len(self.data)):
- for y in range(0, len(self.data[x])):
- if len(current_table[y]) > 0 and len(self.data[x][y]) > 0:
- forward = 0
- break
- if forward:
- self.add_line(current_table, x)
- for letter in current_table:
- if len(letter):
- counter += 1
- if counter == solution:
- self.solutions.append("".join(current_table))
- else:
- if counter < solution:
- self.solve(current_table, x)
- self.delete_line(current_table, x)
- forward = 1
- counter = 0
- def find(self):
- del self.solutions[:]
- self.solve(['']*len(self.data[0]))
- return self.solutions
- class StartTheRocket:
- data = []
- xAlgorithm = None
- def __init__(self):
- self.xAlgorithm = XAlgorithm()
- def random_word(self, length):
- string = ""
- count_spaces = 0
- count = 0
- for x in range(0, length):
- if random.randint(0, 1):
- string += " "
- else:
- string += random.choice('qwertyuiopasdfghjklzxcvbnm')
- count += 1
- if count == length:
- break
- return string
- def prepare_data(self):
- string = ""
- for item in self.data:
- for letter in item:
- if len(letter) > 0:
- string += letter
- else:
- string += " "
- string += "\n"
- return string
- def execute(self):
- command = 'echo "'+str(self.prepare_data())+'" | ./'+str(sys.argv[1])
- output = subprocess.check_output(command, shell=True)
- output = str(output)[2:].split("\\n")
- result_data = []
- for result in output:
- if len(result) == len(self.data[0]):
- result_data.append(result)
- return result_data
- def rocket_check(self, data):
- self.data = data
- self.xAlgorithm.load_data(data)
- original_data = self.execute()
- check_data = self.xAlgorithm.find()
- for result in original_data:
- if result not in check_data:
- printf("Error! No '"+str(result)+'" in generated checked data.')
- return 1
- return 0
- def milky_way(self):
- x = 10
- while True:
- data = []
- for y in range(0, x):
- data.append(self.random_word(x))
- print("TEST "+str(x)+":")
- if not self.rocket_check(data):
- print("OK!\n")
- x += 1
- rocket = StartTheRocket()
- rocket.milky_way()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement