Skip to content

Instantly share code, notes, and snippets.

@miku
Created December 21, 2009 13:11
Show Gist options
  • Save miku/260940 to your computer and use it in GitHub Desktop.
Save miku/260940 to your computer and use it in GitHub Desktop.
#!/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