18
\$\begingroup\$

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.

\$\endgroup\$

marked as duplicate by jimmy23013, Sriotchilism O'Zaic code-golf May 29 at 2:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

21 Answers 21

10
\$\begingroup\$

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.

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

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 ]
\$\endgroup\$
6
\$\begingroup\$

Haskell, 25 bytes

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

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Jelly, 3 bytes

sZẈ

Try it online!

How?

sZẈ - Link: integer tasks, integer workers
    - implicit range(tasks)
s   - split into chunks of length workers
 Z  - transpose
  Ẉ - length of each
\$\endgroup\$
3
\$\begingroup\$

Python 2, 40 38 36 Bytes

-2 bytes thanks to Mr. Xcoder

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

Try it Online!

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

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.

\$\endgroup\$
  • \$\begingroup\$ On mobile didn't see other answers already... takes this to 3 too \$\endgroup\$ – Jonathan Allan Aug 15 '18 at 21:22
  • \$\begingroup\$ @JonathanAllan Thanks, I wonder how I never saw that built-in. \$\endgroup\$ – Mr. Xcoder Aug 15 '18 at 21:25
  • \$\begingroup\$ Only been around for a couple of months I think \$\endgroup\$ – Jonathan Allan Aug 15 '18 at 21:26
  • \$\begingroup\$ 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! \$\endgroup\$ – Mr. Xcoder Aug 15 '18 at 21:34
2
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

05AB1E, 7 bytes

LôζðK€g

Try it online or verify all test cases.

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]
\$\endgroup\$
2
\$\begingroup\$

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

Haskell, 41 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Perl 5 -a, 31 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Οurous Aug 16 '18 at 0:24
  • \$\begingroup\$ 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! :) \$\endgroup\$ – Kevin Cruijssen Aug 16 '18 at 7:42
  • \$\begingroup\$ 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. \$\endgroup\$ – Kevin Cruijssen Aug 16 '18 at 7:44
  • \$\begingroup\$ @Οurous Yeah, I missed that, was already late :/ \$\endgroup\$ – cryoW0lf Aug 16 '18 at 9:03
  • \$\begingroup\$ @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 :) \$\endgroup\$ – cryoW0lf Aug 16 '18 at 9:03
1
\$\begingroup\$

Groovy, 35 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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.

Another neat answer is:

19 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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()))))
\$\endgroup\$
1
\$\begingroup\$

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.

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

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.

\$\endgroup\$
0
\$\begingroup\$

Charcoal, 19 bytes

NθNηIEθ⁻÷×⊕ιηθ÷×ιηθ

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

Nθ                  Input team size
  Nη                Input number of tasks
      θ             Team size
     E              Map over implicit range
           ι    ι   Current value
          ⊕         Incremented
            η    η  Number of tasks
         ×     ×    Multiply
             θ    θ Team size
        ÷     ÷     Integer divide
       ⁻            Subtract
    I               Cast to string
                    Implicitly print
\$\endgroup\$
0
\$\begingroup\$

APL (Dyalog), 19 bytes

4 bytes saved thanks to @Adám

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

Try it online!

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

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         
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.