The Challenge
Given a string, output the text in the shape of a square.
You can assume that the text will always fit in a square, and that it will never be an empty string.
You can also assume it will never have newlines.
Example
Input:
Hi, world
Output:
Hi,
wo
rld
Test Cases
Input:
Hi, world! Hello
Output:
Hi,
worl
d! H
ello
Input:
Lorem ipsum dolor sit amt
Output:
Lorem
ipsu
m dol
or si
t amt
Input:
H
Output:
H
Rules
- This is code-golf, so shortest answer in bytes wins! Tiebreaker is most upvoted answer.
- Standard loopholes are forbidden.
-
\$\begingroup\$ Can we assume that the input will never have new lines? \$\endgroup\$bren– bren2016年08月06日 02:13:47 +00:00Commented Aug 6, 2016 at 2:13
-
\$\begingroup\$ @MayorMonty yep. \$\endgroup\$acrolith– acrolith2016年08月06日 02:20:21 +00:00Commented Aug 6, 2016 at 2:20
-
2\$\begingroup\$ Can we output array of strings instead? \$\endgroup\$Leaky Nun– Leaky Nun2016年08月06日 05:10:09 +00:00Commented Aug 6, 2016 at 5:10
-
\$\begingroup\$ @LeakyNun no 15 chars \$\endgroup\$acrolith– acrolith2016年08月06日 16:58:13 +00:00Commented Aug 6, 2016 at 16:58
-
2\$\begingroup\$ May we print with a trailing newline? \$\endgroup\$Giuseppe– Giuseppe2017年10月04日 15:53:54 +00:00Commented Oct 4, 2017 at 15:53
72 Answers 72
Lua, 65 bytes
t=io.read()for i in t:gmatch(('.'):rep((#t)^0.5)) do print(i) end
Explanation:
t=io.read() -- Read input string
for i in -- Iterator-based loop
t:gmatch( -- over results of pattern-matching on input
('.'):rep( -- Make string of dots
(#t)^0.5) -- square root from input length string long
) -- ...which is a pattern for matching that long substring
)
do -- Loop body
print(i) -- Print our match and newline
end
TL;DR: it uses pattern-matching (Lua version of regexp) to get square root of input long substrings and prints them.
Lua, 84 bytes (with UTF-8 support)
t=io.read()for i in t:gmatch(('.[128円-191円]*'):rep(utf8.len(t)^0.5)) do print(i) end
Same idea as above, but with few UTF-8 tricks.
Runic Enchantments, 19 bytes
qA,r\;>i:l͍'
$ka$L
Outputs with a trailing newline because its cheaper to loop and wait for stack underflow to cause program termination than to figure out if anything's left on the stack to print. As such the required terminator ; goes unexecuted.
Inputs that are not capable of being square are output as tall rectangles, as computation required to coerce a square through trailing spaces aren't necessary due to challenge spec regarding inputs.
Additionally, spaces need to be escaped in order for the input to be treated as a single string (rather than multiple space-separated strings).
Elm 0.19, 112 bytes
import List.Extra as L
s l=List.concat<|List.intersperse['\n']<|L.groupsOf(floor<|sqrt<|toFloat<|List.length l)l
Input and output as a list of characters. See it working here.
Tcl, (削除) 100 (削除ここまで) (削除) 98 (削除ここまで) 65 bytes
{s {regsub -all -- (.{[expr int([string le $s]**.5)]}) $s \1円\n}}
C#, 180 bytes
using System;public class P{public static void Main(string[]a){var d=(int)Math.Sqrt(a[0].Length);string f="";for(int i=0;i<d;i++){f+=a[0].Substring(i*d,d)+"\n";}Console.Write(f);}}
MathGolf, 5 bytes
h√i/n
Unnecessarily long, the i shouldn't be needed in my opinion. For operators where float behavior is undefined, the float is normally cast to an integer and used in the operation. This seems to be forgotten for string division, which leads to the extra byte.
Explanation
h length of array/string without popping
√ pop a : push(sqrt(a))
i convert to integer
/ split strings
n map array with newlines
Python 3, 54 bytes
lambda s:fill(s,int(len(s)**.5))
from textwrap import*
In Python 3 the textwrap module can be used to perform this task.
Factor + math.unicode, (削除) 39 (削除ここまで) 38 bytes
[ dup length √ 1 /i group "\n"join ]
Explanation:
dupDuplicate the input.Stack: (e.g.)
"Hi, world" "Hi, world"lengthGet the length.Stack:
"Hi, world" 9√Take the square root.Stack:
"Hi, world" 3.01 /iConvert to an integer. This is shorter than>integer.Stack:
"Hi, world" 3groupSplit a sequence into groups indicated by the integer on top of the data stack.Stack:
{ "Hi," " wo" "rld" }"\n"joinJoin each string in a sequence into a newline-delimited string.Stack:
"Hi,\n wo\nrld"
Brachylog, 7 bytes
ẹġṁcmẉm
Explanation
ẹ Convert the input string into a list of characters
ġ Split that list into groups of equal length
ṁ The result must be a square matrix
cm Concatenate each row back into a string
ẉm Print each row with a trailing newline
The solution would be 5 bytes if lists of lines were acceptable output, or 3 bytes if a list of lists of characters were acceptable.
ISOLADOS, 93 bytes
ISOLAAAADOS ISOLAAAAAADOS ISOLAAAAAAAADOOS ISOLAAADOOOOOOS ISOLAAAAAAAADOOOS ISOLAAAAAAAAADOS
ISOLAAAADOS input
ISOLAAAAAADOS duplicate
ISOLAAAAAAAADOOS length
ISOLAAADOOOOOOS square root
ISOLAAAAAAAADOOOS a split into chunks of length b
ISOLAAAAAAAAADOS join by new lines
Thunno 2 N, 3 bytes
lƭ/
Explanation
lƭ/ # Implicit input
l # Length of input
ƭ # Square root
/ # Split into that
# many pieces
# Implicit output,
# joined on newlines