Last active
April 26, 2016 02:07
-
-
Save smopucilowski/39151e6de3cce8ed394fd52b4c11d46a to your computer and use it in GitHub Desktop.
MUMS numbers game solver
This file contains hidden or 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 combinations, combinations_with_replacement, permutations | |
from operator import add, sub, mul, truediv | |
target = 888 | |
numbers = (75, 25, 9, 8, 7, 6) | |
operations = (add, sub, mul, truediv) | |
def evaluation_tree(operators, numbers): | |
# op0 | |
# / \ | |
# / op1 | |
# / / \ | |
# op2 op3 op4 | |
# / \ / \ / \ | |
#n0 n1 n2 n3 n4 n5 | |
try: | |
leaf2 = operators[2](numbers[0], numbers[1]) | |
leaf3 = operators[3](numbers[2], numbers[3]) | |
leaf4 = operators[4](numbers[4], numbers[5]) | |
leaf1 = operators[1](leaf3, leaf4) | |
leaf0 = operators[0](leaf2, leaf1) | |
return leaf0 | |
except ZeroDivisionError: | |
return None | |
for sel_operators in combinations_with_replacement(operations, 5): | |
for operators in permutations(sel_operators, 5): | |
for number in permutations(numbers, 6): | |
result = evaluation_tree(operators, number) | |
if result == target: | |
print('One solution: %s with %s gives %s' % (str(operators), str(number), str(result))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment