[Python-ideas] Yet another sum function (fractions.sum)

David Mertz mertz at gnosis.cx
Wed Aug 21 02:16:09 CEST 2013


On Tue, Aug 20, 2013 at 3:26 PM, Oscar Benjamin
<oscar.j.benjamin at gmail.com>wrote:

> So with all our powers combined I created binsortmergesum (bsms) and
> it looks like this:
>
> def bsms(iterable):
>     bins = defaultdict(int)
>     for num in iterable:
>         bins[num.denominator] += num.numerator
>     return mergesum([F(n, d) for d, n in sorted(bins.items())])
>

Wow.  I noticed that you didn't do a sort in your binsum(), but I didn't
think it would make much difference.  Actually, I thought the cost of the
sort wouldn't be worth it at all.  It's actually a little unclear to me
exactly why it makes such a big difference as it does in the 'e' case.

Still, I love that function.  It's simple, elegant, and fast (when used on
Fraction, of course; not everywhere).


-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130820/6e0f6957/attachment.html>


More information about the Python-ideas mailing list