# [melbourne-pug] Friday Afternoon Python

martin schweitzer schweitzer at acm.org
Sat Aug 22 05:01:24 CEST 2009

```> e = "".join([ s[j]+"+"*(i&2**j>0) for j in xrange(n) ])

Brilliant!  I was trying to do something similar, but did not have the brain
bandwidth yesterday afternoon.

One simplification that I did add once I had sets available (wrote the
earlier version using Python 2.3), was to replace the last few lines with:

for key in set(lhs.keys()) & set(rhs.keys()):
for v in lhs[key]:
for u in rhs[key]:
print v + ' = ' + u

It may be argued that it neither makes it more readable nor more efficient -
however my personal feeling is that it does express my intent better (ie. I
am trying to find the set of keys that are in the intersection of the lhs
and rhs).

Okay, now to go and get the ingredients to make Tom Yum...

Regards,
Martin

On Sat, Aug 22, 2009 at 12:35 PM, John La Rooy <john.larooy at gmail.com>wrote:

> 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)
>
>
> _______________________________________________
> melbourne-pug mailing list
> melbourne-pug at python.org
> http://mail.python.org/mailman/listinfo/melbourne-pug
>
>

--
Martin Schweitzer
Mobile: 0412 345 938
E-mail: schweitzer at acm.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/melbourne-pug/attachments/20090822/eeeebf1f/attachment.htm>
```