Interesting math problem
Arnaud Delobelle
arnodel at googlemail.com
Mon Mar 17 18:57:47 EDT 2008
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)]
>>> slope(130, 50)
[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]
>>> len(slope(130, 50))
50
>>> sum(slope(130, 50))
130
>>>
Smugly yours
--
Arnaud
More information about the Python-list
mailing list