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.
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
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.
N M string0 string1 string2 string3 string4 string5 ... string(N-1)
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.
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.
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
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.
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
Please mark your answer as : Language - XX bytes.
It is code-golf, 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.