Intro
More complex than A square of text since this requires padding and input has unknown data type.
Every year, Dyalog Ltd. holds a student competition. The challenge there is to write good APL code. This is a language agnostic code-golf edition of this year's tenth problem.
I have explicit permission to post this challenge here from the original author of the competition. Feel free to verify by following the provided link and contacting the author.
Problem
Write a program/function that will reshape a given string or numeric list into the smallest square that will contain all the elements of the input, padding with additional elements if necessary. The pad element should be the default fill element for the given data type, or any one element of your choice. The elements of the square should be in such order that flattening it will give the original order of input data (with trailing padding elements, if any).
Test cases
[1,2,3,4]
→
[[1,2],
[3,4]]
[1,2,3,4,5]
→
[[1,2,3],
[4,5,0],
[0,0,0]]
"Dyalog APL"
→
[["Dyal"], [["D","y","a","l"],
["og A"], or ["o","g"," ","A"],
["PL "], ["P","L"," "," "],
[" "]] [" "," "," "," "]]
[100]
→
[[100]]
[]
→
your language's closest equivalent to an empty matrix, e.g. []
or [[]]
[1,2,3,4,'O']
, or is it guaranteed not to occur? 3. Is the required order after flattening requirement ignoring the pad elements (e.g. could an input of[1,2,3,4,5]
yield[[0,0,0],[0,1,2],[3,4,5]]
or even[[0,1,0],[2,0,3],[0,4,5]]
)? \$\endgroup\$