Marc Christiansen wrote:
> sturlamolden <[EMAIL PROTECTED]> wrote:
>> On 18 Mar, 00:58, Jeff Schwab <[EMAIL PROTECTED]> wrote:
>>
>>> def make_slope(distance, parts):
>>>      if parts == 0:
>>>          return []
>>>
>>>      q, r = divmod(distance, parts)
>>>
>>>      if r and parts % r:
>>>          q += 1
>>>
>>>      return [q] + make_slope(distance - q, parts - 1)
>> Beautiful. If Python could optimize tail recursion, it would even run
>> fast.
> 
> This was my first thought, too. But tailcall optimisation wouldn't help
> here. `make_slope` is not tail recursive, the `+` (aka list.extend) gets
> executed after the recursion. 


def make_slope(distance, parts, L=()):
     if parts == 0:
         return L

     q, r = divmod(distance, parts)

     if r and parts % r:
         q += 1

     return make_slope(distance - q, parts - 1, (q,) + L)
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to