penny distribution

Jess Austin austin at smartobject.biz
Wed Nov 5 03:44:10 EST 2003


"Batista, Facundo" <FBatista at uniFON.com.ar> wrote in message news:<mailman.282.1066701476.2192.python-list at python.org>...
> #- Alex Martelli wrote:
> #- Excellent.  You can also provide a .divideBy(N) method that returns
> #- an N-items list, distributing the excess pennies (or whatever units);
> #- an URL by Martin Fowler that was quoted on the thread shows that.
> 
> Very interesting (and useful). Adding to the PEP.
> 
> .	Facundo

A similar but more general use-case arises when you have to distribute
a product n ways.  E.g., when we apply a tax rate r to a total bill q
the total tax is r*q.  But if we have to indicate what portion of r*q
applies to each line item on the bill, then we require a similar sort
of penny-distribution.  Here is a function prototype:

distribute_product(multiplier, *portions)

This function would return a list of amounts, such that:

distribute_product(m, a, b, c) ~= [m*a, m*b, m*c]   _and_
sum(distribute_product(m, a, b, c)) = m*(a+b+c)

I.e., the first equality is approximate but the second is strict.  You
could define Alex's divideBy method as:

def divideBy(self, N):
    return distribute_product(self, *[1.0/N for i in range(N)])

This situation arises, for example, in U.S. telecom billing, which is
notoriously complex, for reasons various and arcane.  Most of the
reasons have to do with the inability of any customer or regulator to
overlook a penny as the EU seems to have quite reasonably mandated.

later,
Jess




More information about the Python-list mailing list