DNPotapov / Codewars-katas-

0 stars 0 forks source link

Almost Even (6 kyu) #25

Open DNPotapov opened 1 year ago

DNPotapov commented 1 year ago
def split_integer(num, parts):
    res = []
    if parts == 1:
        res.append(num)
    else:
        while parts > 0:
            res.append(num // parts)
            num = num - num // parts
            parts = parts - 1
    return res
DNPotapov commented 1 year ago

We need the ability to divide an unknown integer into a given number of even parts — or at least as even as they can be. The sum of the parts should be the original value, but each part should be an integer, and they should be as close as possible.

Example code:

split_integer(20, 6) # returns [3, 3, 3, 3, 4, 4] Complete the function so that it returns an array of integer representing the parts. Ignoring the order of the parts, there is only one valid solution for each input to your function!

(Also, there is no reason to test for edge cases: the input to your function will always be valid for this kata.)

DNPotapov commented 1 year ago

https://www.codewars.com/kata/529e2e1f16cb0fcccb000a6b