A zigzag scan is a method of encoding 2D information in one dimension:
Write a function or program that, given a one-dimensional string of printable ASCII characters, will reverse the zigzag scan to arrange its characters into a square.
For example, given the input string
ABeIfCDgJmnKhLop, your program should return:
ABCD efgh IJKL mnop
Here it is with the zigzag illustrated. The green line shows the order of the original characters, starting at the top left:
The side length 𝒔 of the output (i.e. the number of rows and columns) for an input string of length 𝒏 must equal ⌈√𝒏̅⌉ (
ceil(sqrt(n))). The zigzag pattern is slightly different for even and odd values of 𝒔. Note how to upper right corner of the zigzag in the below example (𝒔 = 5) differs from the above (𝒔 = 4).
Given the string
Woamnlfgasa d gAeMrtouz, your output should be:
Wolfg ang A madeu s Moz art
This is code-golf. The solution with the fewest bytes win.
Input must be taken as a single string or list of characters. It may not have any extra whitespace, but for a single trailing newline. The input (sans newline) will have length 1 ≤ 𝒏 ≤ 4,096.
Output must be a string with multiple lines, a list of strings, or a list of lists of characters. Trailing whitespace (any amount) on each line is allowed but optional, as is a single trailing newline.
Standard loopholes are forbidden.
The below test cases are formatted as:
Input Output ...
PPCG PP CG
foobarbaz for oab baz
Ooo... ...ooO Oo. o. . . .o .oO
OOo.oOOo.oOo.oOo.oOOo.oOO OOOOO ooooo ..... ooooo OOOOO
PrmPmogiusC znragzaooldnl edef G Progra mming Puzzle s and Code G olf
The quick brown fox jumps over the lazy dog Thuin v eqcwfoe koo ry rxs z b ptad jmhlo ue g