Interesting math problem

BJörn Lindqvist bjourne at gmail.com
Wed Mar 19 06:17:18 EDT 2008


On Mon, Mar 17, 2008 at 11:57 PM, Arnaud Delobelle
<arnodel at googlemail.com> wrote:
>  > def make_slope(distance, parts):
>  >     step = distance / float(parts)
>  >     intstep = int(step)
>  >     floatstep = step - intstep
>  >
>  >     steps = []
>  >     acc = 0.0
>  >     for i in range(parts):
>  >         acc += floatstep
>  >         step = intstep
>  >         if acc > 0.999:
>  >             step += 1
>  >             acc -= 1.0
>  >         steps.append(step)
>  >     return steps
>
>  OK then, using list comprehensions.  It is more succint, is it easier
>  to read?
>
>  def slope(dist, parts):
>     return [(i+1)*dist/parts - i*dist/parts for i in xrange(parts)]

Congratulations! You Won! Jeff Schwab's recursive approach is also
cool but this is the most interesting abuse of integer division I have
seen. I don't think any of the variants are readable at a first
glance, but with a comment it should be ok.


-- 
mvh Björn



More information about the Python-list mailing list