Created
December 21, 2009 13:11
-
-
Save miku/260940 to your computer and use it in GitHub Desktop.
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/env python | |
# http://stackoverflow.com/questions/1939902 | |
from __future__ import division | |
import itertools | |
operands = [1, 2, 3, 5, 7] | |
operators = ['+', '-', '*', '/'] | |
def blend(operands, operators): | |
r = [] | |
for i in range(len(operators)): | |
r.append(operands[i]) | |
r.append(operators[i]) | |
r.append(operands[-1]) | |
return r | |
for j in itertools.product(operators, repeat=len(operands) - 1): | |
for i in itertools.permutations(operands): | |
expression = ' '.join(map(str, blend(i, j))) | |
print expression, '=', eval(expression) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment