# Divide the work [duplicate]

There is a job which can be decomposed into x equally-sized smaller tasks. You have a team of size y <= x, where every member works equally fast on any task. The goal for this challenge is to divide the work as evenly as possible such that every member of your team has at least 1 task to perform. As evenly as possible means that given any member a, the number of tasks it must perform may be at most one more than any other member b. A single task cannot be further divided or worked on simultaneously by two members.

# Input

Your program/function will take as input two positive integers x and y. y is guaranteed to be less than or equal to x. You are free to decide in what order your program will take these inputs. You may take the inputs from any input source desired.

# Output

Your program/function will output a list of positive integers of length y representing the number of tasks each member must perform. The list may be in any order. For example, the following outputs are identical:

2 1 2 1
1 1 2 2
2 2 1 1


Outputs may be to any output sink desired.

# Examples

Each line pair denotes inputs and one possible output. These example inputs specify x first.

1 1
1

4 1
4

4 2
2 2

4 3
2 1 1

4 4
1 1 1 1

10 3
3 4 3

10 7
1 2 2 2 1 1 1


# Scoring

This is code golf; shortest code wins. Standard loopholes apply.

# R, 26 bytes

function(x,y)table(1:x%%y)


Try it online!

Counts the number of occurrence of 1,2,...,y in [1...x] modulus y. 12 bytes golfed by @mnel, and than an additional 6 by @digEmAll.

• I think this can be reduced to 32 bytes with function(v,w)table(rep(1:w,l=v)) – mnel Aug 15 '18 at 22:54
• @mnel indeed! We have a number of active golfeRs these days... join the fun :) – JayCe Aug 15 '18 at 23:20
• Nice approach! I guess this is also valid : 26 bytes – digEmAll Aug 19 '18 at 8:33
• @digEmAll indeed! – JayCe Aug 19 '18 at 20:09

# JavaScript (ES6), 34 bytes

Takes input as (y)(x).

y=>g=x=>y?[k=x/y--|0,...g(x-k)]:[]


Try it online!

### Example for x = 10, y = 3

 Remaining tasks     | # of tasks for next worker | Workers
---------------------+----------------------------+-------------------------------------
O O O O O O O O O O | 10 / 3 = 3.333... -> 3     | [   O O O ] [ pending ] [ pending ]
O O O O O O O - - - |  7 / 2 = 3.5      -> 3     | [   O O O ] [   O O O ] [ pending ]
O O O O - - - - - - |  4 / 1 = 4        -> 4     | [   O O O ] [   O O O ] [ O O O O ]


x#y=map(divy)[x..x+y-1]


Try it online!

# Jelly, 3 bytes

sZẈ


Try it online!

### How?

sZẈ - Link: integer tasks, integer workers
s   - split into chunks of length workers
Z  - transpose
Ẉ - length of each


# Python 2, 4038 36 Bytes

-2 bytes thanks to Mr. Xcoder

lambda x,y:x%y*[x/y+1]+[x/y]*(y-x%y)


Try it Online!

• 36: lambda x,y:x%y*[x/y+1]+[x/y]*(y-x%y) – Mr. Xcoder Aug 15 '18 at 21:26

# Jelly, 3 bytes

œsẈ


Try it online!

-1 byte thanks to Jonathan Allan, showing me a built-in I haven't seen before. œs splits the range $[1 \dots\:x]$ in $y$ similarly sized pieces, then Ẉ retrieves the length of each chunk.

• On mobile didn't see other answers already... Ẉ takes this to 3 too – Jonathan Allan Aug 15 '18 at 21:22
• @JonathanAllan Thanks, I wonder how I never saw that built-in. – Mr. Xcoder Aug 15 '18 at 21:25
• Only been around for a couple of months I think – Jonathan Allan Aug 15 '18 at 21:26
• Hmm, I've actually used it once but have since forgotten about it. Just edited about 3 answers to with this built-in. Thanks for the heads-up! – Mr. Xcoder Aug 15 '18 at 21:34

# C (gcc), 48 bytes

Takes i task size, j team size and returns tasks to array k.

l;f(i,j,k)int*k;{for(l=j;l--;)k[l]=i/j+(i%j>l);}


Try it online!

# Brain-Flak, 68 bytes

({}([{}])<{({}(()))}{}>){({}[()]<({}<{({}<>)<>}>()){<>({}<>)}{}>)}{}


Try it online!

Initializes the stack with y ones, then rolls the stack x-y times, each time adding 1 to the former top of the stack.

# 05AB1E, 7 bytes

LôζðK€g

L          # Create a list in the range [1, first (implicit) input]
#  i.e. 10 → [1,2,3,4,5,6,7,8,9,10]
ô         # Split it in chunks of size second (implicit) input
#  i.e. [1,2,3,4,5,6,7,8,9,10] and 3 → [[1,2,3],[4,5,6],[7,8,9],[10]]
ζ        # Zip, swapping rows and columns (with space as filling character by default)
#  i.e. [[1,2,3],[4,5,6],[7,8,9],[10]] → [[1,4,7,10],[2,5,8,' '],[3,6,9,' ']]
ðK      # Remove all those spaces
#  i.e. [[1,4,7,10],[2,5,8,' '],[3,6,9,' ']]
#   → [['1','4','7','10'],['2','5','8'],['3','6','9']]
€g    # And then take the length of each inner list as result
#  i.e. [['1','4','7','10'],['2','5','8'],['3','6','9']] → [4,3,3]


# MATL, 6 bytes

:gie!s


Try it online!

(implicit input, y)
:                            % range, push 1...x
g                            % convert to logical (all ones)
i                            % push y
e                            % reshape to matrix of y rows, padding with 0s
!s                           % row sums; as a row vector
(implicit output)

• I have created [this meta answer] (codegolf.meta.stackexchange.com/a/16802/80010) as a Community wiki so you can modify it. – JayCe Aug 16 '18 at 13:58
• ^ I hope it will let you upvote it after you modify it... otherwise making this a community wiki was a really bad idea! – JayCe Aug 16 '18 at 14:00

x#y=[div x y+sum[1|i<=mod x y]|i<-[1..y]]


Try it online!

# Perl 5-a, 31 bytes

$a[$_--%$F[1]]++while$_;say"@a"


Try it online!

# Java, 55 bytes

By making use of var in Java 10 we can reduce by 1 byte.

y->x->{var a=new int[y];for(;0<x;a[x--%y]++);return a;}


Try it online!

Java 8 Version with 56 bytes

y->x->{int[]a=new int[y];for(;0<x;a[x--%y]++);return a;}


Works by just iterating over the array representing the workers until no tasks are left.

• Unless it's explicitly stated otherwise in the question (which it isn't here), you'll need to include private static int[] g(int x, int y) { and the closing brace in your byte count. – Οurous Aug 16 '18 at 0:24
• Hi, welcome to PPCG! Although @Οurous is indeed right that snippets aren't allowed and it should be either a function or full program, in Java 8+ just y->x->{/*code here*/} would be enough in this case. So it's 55 bytes: y->x->{var a=new int[y];for(;0<x;a[x--%y]++);return a;}. Nice answer though! After you've changed it to comply to the default rules I will upvote it. Enjoy your stay! :) – Kevin Cruijssen Aug 16 '18 at 7:42
• Oh, and if you haven't done so yet, Tips for golfing in Java and Tips for golving in <all languages> might be interesting to read through. – Kevin Cruijssen Aug 16 '18 at 7:44
• @Οurous Yeah, I missed that, was already late :/ – cryoW0lf Aug 16 '18 at 9:03
• @KevinCruijssen Thanks, I changed that. But with using Lambda it will increase to 62, due to the requirements that lambda parameters have to be effectively final. But thanks :) – cryoW0lf Aug 16 '18 at 9:03

# Groovy, 35 bytes

{x,y->o=[0]*y;while(x--)o[x%y]++;o}


Try it online!

# Brachylog, 14 bytes

⟨{h⟦₁}ġ₎t⟩z₁lᵐ


Try it online!

Port of Jonathan Allan's method - range, split, transpose/zip, length - via the explanation in Kevin Cruijssen's answer. Can also be tT&h⟦₁;Tġ₎z₁lᵐ equivalently.

### 19 bytes

h~+.ℕ₁ᵐ≜⟨⌉-⌋⟩<2&t~l


Try it online!

# UGL 1.0.0, 20 Bytes

CÑORCPFÐWC_+_WNINI


Port of the Haskell answer. (even though the language transpiles to Python)

Takes 2 inputs via stdin.

## Explanation (Warning: This may get out of hand)(update: it did not)

C takes two arguments, and apart from some other functions, it is (lambda x,y:map(x,y)).

Ñ is the alias of the int() function. (When you pass it as parameter instead of feeding arguments to it)

O takes one function with two arguments, and two anything, and applies the function to them.

R defines a lambda with two arguments: lambda _,W: <stuff>

C, again is map.

P is functools.partial

F is flip (lambda f: lambda x,y: f(y,x))

Ð is the alias of Division (/)

W is the second argument of the lambda we defined with R

C is exclusive range this time

_ is the first argument of the lambda we defined with R

+_W is literally _+W

NINI is two inputs taken from stdin and converted to integers.

So, this thing is (functions redefined in order to make the last line a bit short):

import functools as ft
apply2 = lambda x,y,z: x(y,z)
flip = lambda f: lambda x,y: f(y,x)
div = lambda x,y: x/y
rg = range
p = print
ip = input()
p(map(int,apply2(lambda _,W: map(ft.partial(flip(div),W),rg(_,(_+W))),int(ip()),int(ip()))))


# Lua, 50 49 bytes

1 byte saved thanks to DLosc :)

x,y=...repeat print(x//y)x=x-x//y y=y-1until y<=0


Try it online!

The output is decimal and in different lines because of the way Lua implements the function print(), if this is a problem I can edit the submission to use io.write() instead.

• 1 byte savings is possible by eliminating the space between 1 and until. :) – DLosc Sep 21 '18 at 21:01

# Clean, 39 bytes

Not much room for originality in this one.

import StdEnv
$x y=[i/y\\i<-[x..x+y-1]]  Try it online! Defines the function $ :: Int Int -> [Int], always returning higher numbers towards the end.

# Charcoal, 19 bytes

ＮθＮηＩＥθ⁻÷×⊕ιηθ÷×ιηθ


Try it online! Link is to verbose version of code. Based on Bresenham's line algorithm. Explanation:

Ｎθ                  Input team size
θ             Team size
Ｅ              Map over implicit range
ι    ι   Current value
⊕         Incremented
×     ×    Multiply
θ    θ Team size
÷     ÷     Integer divide
⁻            Subtract
Ｉ               Cast to string
Implicitly print


# APL (Dyalog), 19 bytes

4 bytes saved thanks to @Adám

{×⍵:(⍺-1)∇⍵-⎕←⌊⍵÷⍺}


Try it online!

• 0=⍵:⋄×⍵: – Adám Aug 16 '18 at 14:52
• Can't you remove k←? – Adám Aug 16 '18 at 14:58
• @Adám thanks, gone a bit rusty :P – Uriel Aug 16 '18 at 15:04

# Forth (gforth), 47 bytes

: f tuck /mod swap rot 0 do 2dup i > - . loop ;


Try it online!

### Explanation

Divides the work by the number of people, add 1 to all workers whose index is less than (or equal) to the amount of excess work (the remainder/modulus)

### Code Explanation

: f            \ start a new word definition
tuck         \ stick a copy of the number of workers in the "back" of the stack
/mod swap    \ get the quotient and remainder, move remainder to the top
rot          \ grab the copy of the number of workers from earlier and move it to the top
0 do         \ start counted loop from 0 to #workers - 1
2dup       \ duplicate the modulus and minimum work
i >        \ check if the index is greater than the remainder
-          \ subtract from minimum work (forth uses -1 for "true")
.          \ output the result
loop         \ end the counted loop
;              \ end the word definition