Skip to content

Instantly share code, notes, and snippets.

@makimoto
Created June 20, 2010 16:39
Show Gist options
  • Save makimoto/445940 to your computer and use it in GitHub Desktop.
Save makimoto/445940 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
'''
a solver for http://twitter.com/not522/status/16626276702
'''
from sys import argv
from itertools import permutations,product
ops = ['+','-','*','/']
parns = ['', '(', ')' ]
nums = map(float,argv[1:])
for n in permutations(nums):
for o in product(ops, repeat = len(nums) -1):
for p in product(parns, repeat = len(nums) * 2):
e = ''
for i in xrange(len(nums)-1):
e += '%s %f %s %s ' % (p[i * 2], n[i], p[i * 2 - 1], o[i])
e += '%s %f %s' % (p[-2], n[-1], p[-1])
try:
ans = eval(e)
if 3.13 < ans < 3.15:
print(e + ' = %0.2f' % ans)
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment