Divide the work

18

1

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.

helloworld922

Posted 2018-08-15T20:48:50.610

Reputation: 2 503

Question was closed 2019-05-29T02:31:28.987

Answers

10

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.

JayCe

Posted 2018-08-15T20:48:50.610

Reputation: 2 655

I think this can be reduced to 32 bytes with function(v,w)table(rep(1:w,l=v)) – mnel – 2018-08-15T22:54:41.323

3@mnel indeed! We have a number of active golfeRs these days... join the fun :) – JayCe – 2018-08-15T23:20:59.147

Nice approach! I guess this is also valid : 26 bytes

– digEmAll – 2018-08-19T08:33:39.303

@digEmAll indeed! – JayCe – 2018-08-19T20:09:22.470

6

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 ]

Arnauld

Posted 2018-08-15T20:48:50.610

Reputation: 111 334

6

Haskell, 25 bytes

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

Try it online!

xnor

Posted 2018-08-15T20:48:50.610

Reputation: 115 687

3

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!

Zachary Cotton

Posted 2018-08-15T20:48:50.610

Reputation: 679

36: lambda x,y:x%y*[x/y+1]+[x/y]*(y-x%y) – Mr. Xcoder – 2018-08-15T21:26:19.350

3

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

Jonathan Allan

Posted 2018-08-15T20:48:50.610

Reputation: 67 804

2

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.

Mr. Xcoder

Posted 2018-08-15T20:48:50.610

Reputation: 39 774

On mobile didn't see other answers already... takes this to 3 too – Jonathan Allan – 2018-08-15T21:22:45.983

@JonathanAllan Thanks, I wonder how I never saw that built-in. – Mr. Xcoder – 2018-08-15T21:25:24.763

Only been around for a couple of months I think – Jonathan Allan – 2018-08-15T21:26:19.823

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 – 2018-08-15T21:34:22.323

2

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!

ErikF

Posted 2018-08-15T20:48:50.610

Reputation: 2 149

2

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.

Nitrodon

Posted 2018-08-15T20:48:50.610

Reputation: 9 181

2

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]

Kevin Cruijssen

Posted 2018-08-15T20:48:50.610

Reputation: 67 575

2

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)

Giuseppe

Posted 2018-08-15T20:48:50.610

Reputation: 21 077

I have created [this meta answer] (https://codegolf.meta.stackexchange.com/a/16802/80010) as a Community wiki so you can modify it.

– JayCe – 2018-08-16T13:58:48.537

^ I hope it will let you upvote it after you modify it... otherwise making this a community wiki was a really bad idea! – JayCe – 2018-08-16T14:00:17.217

1

Haskell, 41 bytes

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

Try it online!

nimi

Posted 2018-08-15T20:48:50.610

Reputation: 34 639

1

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.

cryoW0lf

Posted 2018-08-15T20:48:50.610

Reputation: 11

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 – 2018-08-16T00:24:43.057

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 – 2018-08-16T07:42:39.640

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 – 2018-08-16T07:44:00.387

@Οurous Yeah, I missed that, was already late :/ – cryoW0lf – 2018-08-16T09:03:26.953

@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 – 2018-08-16T09:03:43.073

@cryoW0lf That's indeed the case when you use x->y->. However, in the code I linked above I use y->x->. :) Only the first of the currying lambda needs to be effectively final, the inner one can be modified. PS: You can remove the space at int[]a in your Java 8 version. – Kevin Cruijssen – 2018-08-16T09:08:14.003

@KevinCruijssen Oh, nice to know, thanks :o – cryoW0lf – 2018-08-16T09:14:38.127

1

Perl 5 -a, 31 bytes

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

Try it online!

Xcali

Posted 2018-08-15T20:48:50.610

Reputation: 7 671

1

Groovy, 35 bytes

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

Try it online!

GolfIsAGoodWalkSpoilt

Posted 2018-08-15T20:48:50.610

Reputation: 101

1

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!

sundar - Reinstate Monica

Posted 2018-08-15T20:48:50.610

Reputation: 5 296

1

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

Windmill Cookies

Posted 2018-08-15T20:48:50.610

Reputation: 601

1

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.

Marcio Medeiros

Posted 2018-08-15T20:48:50.610

Reputation: 51

1 byte savings is possible by eliminating the space between 1 and until. :) – DLosc – 2018-09-21T21:01:40.413

0

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.

Οurous

Posted 2018-08-15T20:48:50.610

Reputation: 7 916

0

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

Neil

Posted 2018-08-15T20:48:50.610

Reputation: 95 035

0

APL (Dyalog), 19 bytes

4 bytes saved thanks to @Adám

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

Try it online!

Uriel

Posted 2018-08-15T20:48:50.610

Reputation: 11 708

0=⍵:⋄×⍵: – Adám – 2018-08-16T14:52:56.737

Can't you remove k←? – Adám – 2018-08-16T14:58:12.077

@Adám thanks, gone a bit rusty :P – Uriel – 2018-08-16T15:04:29.847

0

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         

reffu

Posted 2018-08-15T20:48:50.610

Reputation: 1 361