Last active
September 3, 2015 20:00
-
-
Save mmirz719/24d13186de597e6314c8 to your computer and use it in GitHub Desktop.
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
import itertools, re, sys | |
ops = (lambda x, y: x + y, lambda x, y: x - y, lambda x, y: x * y, lambda x, y: x / y) | |
def recurse(so_far, operands, desired): | |
if not operands: | |
return '' if so_far == desired else None | |
for op, op_char in itertools.izip(ops, '+-*/'): | |
expr = recurse(op(so_far, operands[0]), operands[1:], desired) | |
if isinstance(expr, basestring): | |
return ' %c %d%s' % (op_char, operands[0], expr) | |
inputs = [float(i) for i in re.findall('\S+', sys.stdin.readline())] | |
for permuted in itertools.permutations(inputs[:-1]): | |
result = recurse(permuted[0], permuted[1:], inputs[-1]) | |
if result: | |
print '%d%s' % (permuted[0], result) | |
sys.exit() | |
print 'Invalid' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment