Interesting math problem

Ivan Illarionov ivan.illarionov at gmail.com
Tue Mar 18 00:27:18 CET 2008


On Mar 18, 1:24 am, "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

Integer-only version:

def make_slope(distance, parts):
    intstep, err = divmod(distance, parts)

    steps = []
    acc = 0
    for i in range(parts):
        acc += err
        step = intstep
        if acc >= parts:
            step += 1
            acc -= parts
        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



More information about the Python-list mailing list