Homework

April 3, 2020

[ I continue working from home. It is interesting that my commute has gone from a daily 75-minute round-trip by car to a 75-foot round-trip by foot, so I think I should have more energy in the evening, but the opposite is true; I am exhausted by the end of the day, just from sitting in front of my laptop. ]

Today’s exercise is somebody’s homework:

Given positive integers C and N, find N numbers that sum up to C and the difference between the highest and the lowest of these number should not be more than one. For example: with C = 26 and N = 7, the desired output is [4 4 4 4 4 3 3].

Your task is to write a program to solve the student’s homework. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

9 Responses to “Homework”

  1. James Curtis-Smith said

    A perl “1-liner”:

    [sourceode lang=”perl”]
    print “@{[ hw(@ARGV) ]}\n”;

    sub hw {
    my($C,$N) = @_;
    return map { int($C/$N) + ($_<$C%$N) } 0..($N-1);
    }
    [/sourcecode]

  2. chaw said

    A simple solution in standard R7RS Scheme and a couple of popular
    helpers.

    (import (scheme base)
            (scheme write)
            (only (srfi 8) receive)
            (only (srfi 1) list-tabulate))
    
    (define (homework c n)
      (receive (q r) (floor/ c n)
        (list-tabulate n (lambda (i) (if (< i r) (+ q 1) q)))))
    
    (display (homework 26 7))
    (newline)
    

    Output:

    (4 4 4 4 4 3 3)
    

  3. Daniel said

    Here’s a solution in Python.

    def homework(C, N):
        q, r = divmod(C, N)
        return [q + (x < r) for x in range(N)]
    
    print(homework(26, 7))
    

    Output:

    [4, 4, 4, 4, 4, 3, 3]
    
  4. Globules said

    Here’s a Haskell version.

    -- A list of n numbers whose sum is c, each of which differs by at most 1 from
    -- all the others.  We assume n >= 1.
    sumTo :: Int -> Int -> [Int]
    sumTo c n = let (q, r) = c `divMod` n in replicate r (q+1) ++ replicate (n-r) q
    
    test :: Int -> Int -> IO ()
    test c n = do
      let xs = sumTo c n
      putStrLn $ show c ++ " " ++ show n ++ ": " ++
                 show xs ++ " sum to " ++ show (sum xs)
    
    main :: IO ()
    main = do
      test 26  7
      test 18  3
      test  5 10
      test  0  2
      -- Negative numbers work, too.
      test (-8) 3
    
    $ ./homework
    26 7: [4,4,4,4,4,3,3] sum to 26
    18 3: [6,6,6] sum to 18
    5 10: [1,1,1,1,1,0,0,0,0,0] sum to 5
    0 2: [0,0] sum to 0
    -8 3: [-2,-3,-3] sum to -8
    
  5. def homework(N, C):
        for k in range(1, N+1):
            a = (k-N+C)/N
            if a==int(a):
                return [int(a)]*k + [int(a)+1]*(N-k)
    
    homework(7, 26) #=> [3, 3, 4, 4, 4, 4, 4]
    homework(2, 25) #=> [12, 13]
    homework(3, -8)  #=> [-3, -3, -2]
    
  6. Dave said

    def homework(C, N):
    if C % N == 0:
    answer = [C / N] * N
    return answer
    else:
    remainder = C % N
    C -= remainder
    answer = [C / N] * N
    for i in range(remainder):
    answer[i] += 1
    return answer

    print(solver(26,7))

  7. Jan Van lent said

    Using Python with + for concatenation of lists and * for repetition ([1] * 3 == [1, 1, 1]).

    [C//N+1]*(C%N) + [C//N]*(N-C%N)
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: