3
\$\begingroup\$

The problem

You just got a new job, hurray! You expect to be given various interesting tasks, and be able to learn a lot in the next few months. Good news, your boss gave you your first work this morning. You are now in charge of... Writting adresses on envelopes. Yay. Fortunatelly, you are not required to write everything by hand : there is a software for this, in which you can enter the address as plain text.

However, the software is not really well-made and very old. When you opened the files, every information was scared on several lines, and do not fit anymore on the envelope format you were given.

Definitely not wanting to repare this by hand, you decide to write a small programm that will put the data back in the right format for you.


Your task

Distribute the input on the right amount of lines. Each line should have the same amount of inputs (regardless of their size in characters) when possible, and if not, the number of line with different lengths should be limited to its minimum. Meaning that the distribution is to be made in terms of inputs and not characters.

For cases where two solutions are possible (7 inputs on 3 lines for example), the algorithm should maximise the number of inputs in the first lines (starting from the top). There is an example for this very case.


Input format

N M
string0 
string1 
string2 
string3 
string4 
string5 
...
string(N-1) 

Where :

N is an int and corresponds to the number of fragments in which the data was scattered.

M is an int and corresponds to the number of lines in which the data should be. You may assume that M<=N if necessary.

string0 ... string(N-1) are strings that can contain any non-control ASCII character, including spaces and end with [space]\n. You thus do not need do add spaces between the strings in the output. You may also assume that there is no horizontal length limit.

They do not contain any line break except the final one.


Examples

Input            Output
3 1            string0 string1 string2
string0 
string1 
string2 
5 3            AA BBB
AA             C DDDDD
BBB            EEEE
C      
DDDDD      
EEEE
8 2            AAAAA BBB C DDD
AAAAA          EE FFF GGGG HH
BBB            
C
DDD
EE
FFF
GGGG
HH
8 3           AAA BBB CCC 
AAA           DDD EEE FFF
BBB           GGG HHH
CCC
DDD
EEE
FFF
GGG
HHH
7 3            string0 string1 string2 
string0        string3 string4 string5 
string1        string6 
string2 
string3 
string4 
string5 
string6 

Test cases

5 3
This 
is 
the 
first 
test case 
8 6
Good Night, Good night! 
Parting 
is 
such sweet 
sorrow, 
that I shall 
say good night 
till it be morrow. 
8 3
I 
am starting 
to 
run 
out 
of 
ideas for 
test cases. 

Expected outputs:

This is  //2 strings [This ] and [is ]
the first //2 strings [the ] and [first ]
test case //1 string [test case ]
Good Night, Good night! Parting //2 strings [Good Night, Good night! ] and [Parting ]
is such sweet //2 strings [is such ] and [sweet ]
sorrow, //1 string [sorrow, ]
that I shall //1 string [that I shall ]
say good night //1 string [say good night ]
till it be morrow. //1 string [till it be morrow. ]
I am starting to //3 strings
run out of //3 strings
ideas for test cases. //2 strings

Rules

Please mark your answer as : Language - XX bytes.

It is , so the winner is the answer with the smallest score in bytes.

It would also be very nice if you could provide even briefs explainations, as it is always very enlightning to read this kind of post.


\$\endgroup\$
  • \$\begingroup\$ "any ASCII character", even line breaks like CR and LF? How about null-bytes? \$\endgroup\$ – Adám Jul 12 at 18:27
  • \$\begingroup\$ Please clarify the number of line with different lengths should be limited to its minimum. Does it mean we should break lines to minimise the number of existing line lengths or that all lines should have the same length except for one, and if so, does it matter which one? \$\endgroup\$ – Adám Jul 12 at 19:09
  • \$\begingroup\$ Thank you for your comments, answer, and recommandations. It is indeed very unclear, (unlike what I thought). I updated the post so it becomes more understandable (let me know if it is not). Thank you for taking the time to let me know what I made wrong. ^-^ \$\endgroup\$ – Erel Jul 12 at 21:17
  • \$\begingroup\$ My pleasure. For the future, consider using the sandbox to iron out issues such as these before posting. \$\endgroup\$ – Adám Jul 12 at 21:18
  • \$\begingroup\$ With N=7 and N=3 should the result lines have 3 3 1 or 3 2 2 inputs? \$\endgroup\$ – Adám Jul 12 at 21:23
4
\$\begingroup\$

K (ngn/k), 15 bytes

Anonymous lambda. Takes N, M, and strings as arguments.

{,/'(&~y!!x)_z}

Try it online!

{} lambda; x is N, y is M, z is strings:

 ()_z cut list of strings before the following indices:

  !x enumerate strings

  y! division remainder when divided by target number of lines

  ~ mask indicating zeros

  & indices of trues in mask

 ,/' concatenate elements of each list

| improve this answer | |
\$\endgroup\$
4
\$\begingroup\$

05AB1E, 23 21 bytes

This is my first 05AB1E program (In fact this is my first golfing using golfing language), so please, if you have any notes, tips or improvements - I would love to get some in the comments section.

Program:

`rs/î©\õ.gGs«N®%0Qi¶«}}

Explaination:

`rs/î©\õ.gGs«N®%0Qi¶«}}
`                             Push each element of the input to the stack
 r                            Reverse the stack
  s                           Swap the first two values in stack (which are the numbers)
   /                          Division between both numbers in order to get the number of words in each line
    î                         Round up
     ©                        Store that value in the register
      \                       Remove that number from the stack (remove head)
       õ                      Push empty string to pass over the first iteration
        .g                    Push the length of the stack (which is the number of words)
          G            }      Loop from 1 to The length of the stack
           s                  Swap the two words in the head of the stack
            «                 Concatenate
             N                Push counter_variable
              ®               Push the number we saved in the register (number of words per line)
               %              Modulo (To check if we need to concatenate a newline)
                0Q            Equals zero
                  i  }        If true
                   ¶          Push newline
                    «         Concatenate

Try it online!

Saving 2 bytes with the help of @KevinCruijssen:

`rs/îUõ.gGs«NX%_i¶«}}

Try it online!

| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ 8 bytes, takes input as string M N. \$\endgroup\$ – user92069 Jul 13 at 1:55
  • 2
    \$\begingroup\$ @Third-party'Chef' The ' could be the space-builtin ð. :) Although in this case, removing ' ý is shorter, since » implicitly joins inner lists by spaces before joining by newlines: 5 bytes - try it online. \$\endgroup\$ – Kevin Cruijssen Jul 13 at 7:12
  • 1
    \$\begingroup\$ @SomoKRoceS Some 05AB1E tips can be found here. As for your current implementation: 1) The ©\ +® could be U+X or V+Y for -1 (which pops and stores the value in X/Y respectively, whereas © doesn't pop). 2) There is a builtin for 0Q, which is _. Good luck with future challenges. :) If you have any questions, feel free to ask. \$\endgroup\$ – Kevin Cruijssen Jul 13 at 7:17
  • 1
    \$\begingroup\$ @KevinCruijssen WTF, it's the first tip of the small 05AB1E golfing tips! \$\endgroup\$ – user92069 Jul 13 at 7:27
  • 2
    \$\begingroup\$ Either is fine and it's up to us and yourself. :) If @Third-party'Chef' want to post his 8-byter (which I golfed to 5 bytes) as a separated answer, he's free to do so. If not, you could edit in his answer and my golf with credit indeed, although I would also still leave your original answer as well, since it would be a waste to just remove it after you took the effort in writing it (kinda like what I did here - two programs with different approaches in one answer). \$\endgroup\$ – Kevin Cruijssen Jul 13 at 8:20
3
\$\begingroup\$

APL (dzaima/APL), 18 bytes

Anonymous tacit infix lambda. Takes N as right argument and M as left argument. Prompts stdin for strings as list of character vectors.

{,/⍺⍬⍴⎕,(⍺|-⍵)⍴⊂⍬}

Try it online!

{} "dfn"; left and right arguments are and :

 ⊂⍬ enclose the empty list to treat it as a whole

 ()⍴ cyclically reshape it to the following length:

  -⍵ negate the number of inputs

  ⍺| division remainder when divided by the number of output lines

 ⎕, concatenate that many empty list to the string from stdin

 ⍺⍬⍴ reshape to M rows and as many columns as needed

 ,/ concatenate across

| improve this answer | |
\$\endgroup\$

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