Given a list (array) of names, your challenge is to join the items using some glue and abbreviate the longest items first until the joined string fits a maximum length. And output that string. Since the glue is part of the final string, the length of the glue is included too
Abbreviation is done using the
For example when
| is the glue and 20 is the maximum length
giraffe(length = 7)
giraffe, Spiny lumpsuckerwould become
giraffe|Spiny lumps…(length = 20)
giraffe, Spiny lumpsucker, rhinoceroswould become
giraf…|Spiny…|rhino…(length = 20)
giraffe, Spiny lumpsucker, rhinoceros, Hellbenderwould become any of these:
gir…|Spi…|rhi…|Hell…(all length = 20)
The list, glue and maximum length are input parameters of the program.
When abbreviation is needed, abbreviation must be done on any of the longest items first. When the longest items are equally long, it doesn't matter which you abbreviate.
The output length is smaller than the maximum length when no abbreviation was necessary. When abbreviation needs to occur, the length of the output is exactly equal to the maximum length.
The order of the (abbreviated) items must match the order of the items in the input
You can't assume that the abbreviation character
… and the glue won't occur as characters in the items in the list.
I'm afraid that I overlooked the fact that the length of the glue concatenated with many items by itself may result into a string that is longer than the allowed maximum length. Without considering this, your program may run into an infinite loop. I prefer your program to throw an error in this case. I hope that's not too much of an inconvenience for the ones who already wrote an answer or that are busy writing one. To throw an error is NOT a requirement to win, but I would appreciate if you add a variant that does.
This is code-golf, so the shortest valid answer – measured in bytes – wins.