Skip to content

Instantly share code, notes, and snippets.

@mmirz719
Last active August 29, 2015 14:19
Show Gist options
  • Save mmirz719/d1c064eeb30afefc86b6 to your computer and use it in GitHub Desktop.
Save mmirz719/d1c064eeb30afefc86b6 to your computer and use it in GitHub Desktop.
import fileinput
from itertools import permutations, combinations_with_replacement
def challenge(challenge_str):
the_split = challenge_str.strip().split(' ')
if len(the_split) < 2:
print 'Invalid'
return
for d in the_split:
assert d.isdigit()
challenge_tupple, answer = the_split[:len(the_split)-1], int(the_split[-1])
for arg_permutation in permutations(challenge_tupple):
for op_combinations in combinations_with_replacement('+-*/', len(challenge_tupple)-1):
result = [None]*(len(challenge_tupple)+len(challenge_tupple)-1)
result[::2] = arg_permutation
result[1::2] = op_combinations
if eval(''.join(result)) == answer:
print ' '.join(result)
return
print 'Invalid'
if __name__ == '__main__':
for line in fileinput.input():
challenge(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment