Created
April 14, 2021 02:31
-
-
Save ahmed4end/389a5476aaa7cd2983d945dbef5a50b4 to your computer and use it in GitHub Desktop.
i generate numerical miracles.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import permutations as perms | |
from itertools import product | |
class miracle: | |
''' | |
i make numerical miracles | |
''' | |
def __init__(self): | |
self.ops = ['+', '-', '*', '/'] | |
self.nums = [21, 4, 13, 5, 1] | |
self.miracle_value = 55 | |
self.min_length = len(self.nums) | |
self.max_length = self.min_length + 7 | |
def _zip(self, a, b): | |
max_len = max(len(a), len(b)) | |
a = list(a)+['']*(max_len-len(a)) | |
b = list(b)+['']*(max_len-len(b)) | |
return zip(a,b) | |
def merge_pair(self, nums, ops): | |
res = [] | |
for i, j in self._zip(nums, ops): | |
res.append(str(i)+j) | |
return ''.join(res) | |
def gen_pair(self, length=3): | |
_nums = perms(self.nums, length) | |
_ops = product(self.ops, repeat=length-1) | |
return list(_nums), list(_ops) | |
def get_pop(self, length=3): | |
res = [] | |
nums_arrs, ops_arrs = self.gen_pair(length=length) | |
for ops_arr in ops_arrs: | |
for nums_arr in nums_arrs: | |
res.append(self.merge_pair(nums_arr, ops_arr)) | |
return res | |
def fire(self, ): | |
for length in range(2, self.max_length): | |
print(self.filter_calcs(self.get_pop(length=length))) | |
def filter_calcs(self, exps): | |
res = {} | |
for exp in exps: | |
try: | |
value = eval(exp) | |
if value==self.miracle_value: | |
res[exp] = value | |
except: | |
pass | |
return res | |
if __name__ == '__main__': | |
main = miracle() | |
print(main.fire()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment