Created
December 13, 2010 11:27
-
-
Save bngsudheer/738910 to your computer and use it in GitHub Desktop.
that-anta-heli.py
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
#!/usr/bin/python | |
# Copyright Sudheer Satyanarayana, http://techchorus.net | |
# All rights reserved. | |
# That anta heli problem | |
# a, b, c, d and r are given as input | |
# Solve the problem | |
# a x b y c z d = r | |
# find x, y and z where x, y and z is either +, -, * or / | |
# The equation should be treated as | |
# (( (a x b) y c) z d) = r | |
# Example problem: 45 x 23 y 34 z 1 = 33 | |
# Solution 45 + 23 - 34 - 1 = 33 | |
# Thus the solution is + - - | |
import itertools | |
import sys | |
a = 45 | |
b = 23 | |
c = 34 | |
d = 1 | |
r = 33 | |
# answer = + - - | |
operators = ['addition', 'subtraction', 'multiplication', 'division'] | |
def operate(x, y, operator): | |
if operator == 'addition': | |
return x + y | |
elif operator == 'subtraction': | |
return x - y | |
elif operator == 'multiplication': | |
return x * y | |
elif operator == 'division': | |
return x / y | |
permutations = itertools.product(operators, repeat=3) | |
for permutation_tuple in permutations: | |
r1 = operate(a, b, permutation_tuple[0]) | |
r2 = operate(r1, c, permutation_tuple[1]) | |
r3 = operate(r2, d, permutation_tuple[2]) | |
if r3 == r: | |
print "Found solution" | |
print permutation_tuple | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment