Interesting math problem

Jeff Schwab jeff at schwabcenter.com
Tue Mar 18 05:28:31 CET 2008


Arnaud Delobelle wrote:
> On Mar 17, 10:24 pm, "BJörn Lindqvist" <bjou... at gmail.com> wrote:
>> Here is an interesting math problem:
>>
>> You have a number X > 0 and another number Y > 0. The goal is to
>> divide X into a list with length Y. Each item in the list is an
>> integer. The sum of all integers is X. Each integer is either A or A +
>> 1, those should be "evenly distributed."
>>
>> Example:
>>
>> 17 // 5 = 3 gives the list [3, 3, 4, 3, 4]
>> 16 // 4 = 4 gives the list [4, 4, 4, 4]
>> 113 // 50 = 2 gives the list [2, 3, 2, 3, 3, 2, 3, 2, 3, 3, 2, 3, 2,
>> 3, 3, 2, 3, 2, 3, 3, 2, 3, 2, 3, 3, 2, 3, 2, 3, 3, 2, 3, 2, 3, 3, 2,
>> 3, 2, 3, 3, 2, 3, 2, 3, 3, 2, 3, 2, 3, 3]
>>
>> This algorithm is used a lot in programming. For example, for
>> projecting a line on a pixel display. Your mission, should you choose
>> to accept it, is to improve the code given below which is my best
>> attempt and make it more succinct and easier to read. Preferably by
>> using list comprehensions, map or even reduce....
>>
>> 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
>>
>> # Test code
>> distance = 130
>> parts = 50
>> L = make_slope(distance, parts)
>> assert(len(L) == parts)
>> assert(sum(L) == distance)
>> print L
>>
>> --
>> mvh Björn
> 
> 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)]

That's awesome, but I sure hope you'd mix in a comment in real code. ;)



More information about the Python-list mailing list