## Homework

### April 3, 2020

Here’s my solution:

(define (rept n x) (map (lambda (_) x) (range n)))

(define (f n c) (let ((q (quotient c n)) (r (remainder c n))) (append (rept r (+ q 1)) (rept (- n r) q))))

> (f 7 26) [4 4 4 4 4 3 3)

The student’s solution involved a `for`

loop followed by a `for`

loop embedded in a `while`

loop. He mixed the `rept`

logic with the rest of the program logic and ended up with a mess; separating `rept`

into its own function clearly simplifies the program.

You can run the program at https://ideone.com/TTfLvP.

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]

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

helpers.

Output:

Here’s a solution in Python.

Output:

Here’s a Haskell version.

https://github.com/dennisporterjr/practice-algorithms/blob/master/one-diff-sum.js

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))

Racket: https://github.com/xojoc/programming-praxis/blob/master/2020_04_03.rkt

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