[melbourne-pug] Friday Afternoon Python

John La Rooy john.larooy at gmail.com
Sat Aug 22 04:35:42 CEST 2009


Saturday lunchtime...also using eval because we can

#!/usr/bin/env python
import sys
from collections import defaultdict

def plusses(s, filter=None):
   n=len(s)
   res = defaultdict(list)
   for i in xrange(2**(n-1)):
       e = "".join([ s[j]+"+"*(i&2**j>0) for j in xrange(n) ])
       t = eval(e)
       if filter is None or t in filter:
           res[t].append(e)
   return res

lhs = plusses(sys.argv[1])
rhs = plusses(sys.argv[2],lhs)

for k in rhs.keys():
   for l in lhs[k]:
       for r in rhs[k]:
           print "%s = %s"%(l,r)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/melbourne-pug/attachments/20090822/021a6e13/attachment.htm>


More information about the melbourne-pug mailing list