30
\$\begingroup\$

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 , so shortest answer in bytes wins! Tiebreaker is most upvoted answer.
  • Standard loopholes are forbidden.
asked Aug 5, 2016 at 22:20
\$\endgroup\$
7
  • \$\begingroup\$ Can we assume that the input will never have new lines? \$\endgroup\$ Commented Aug 6, 2016 at 2:13
  • \$\begingroup\$ @MayorMonty yep. \$\endgroup\$ Commented Aug 6, 2016 at 2:20
  • 2
    \$\begingroup\$ Can we output array of strings instead? \$\endgroup\$ Commented Aug 6, 2016 at 5:10
  • \$\begingroup\$ @LeakyNun no 15 chars \$\endgroup\$ Commented Aug 6, 2016 at 16:58
  • 2
    \$\begingroup\$ May we print with a trailing newline? \$\endgroup\$ Commented Oct 4, 2017 at 15:53

72 Answers 72

1 2
3
0
\$\begingroup\$

Lua, 65 bytes

t=io.read()for i in t:gmatch(('.'):rep((#t)^0.5)) do print(i) end

Try it online!

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

Try it online!

Same idea as above, but with few UTF-8 tricks.

answered Jul 9, 2019 at 14:20
\$\endgroup\$
0
\$\begingroup\$

Runic Enchantments, 19 bytes

qA,r\;>i:l͍'
$ka$L

Try it online!

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).

answered Jul 9, 2019 at 15:01
\$\endgroup\$
0
\$\begingroup\$

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.

answered Jul 10, 2019 at 10:42
\$\endgroup\$
0
\$\begingroup\$

Tcl, (削除) 100 (削除ここまで) (削除) 98 (削除ここまで) 65 bytes

{s {regsub -all -- (.{[expr int([string le $s]**.5)]}) $s \1円\n}}

Try it online!

answered Jul 8, 2019 at 1:14
\$\endgroup\$
0
\$\begingroup\$

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);}}

Try Online

\$\endgroup\$
0
\$\begingroup\$

MathGolf, 5 bytes

h√i/n

Try it online!

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
answered Aug 20, 2019 at 10:54
\$\endgroup\$
0
\$\begingroup\$

Python 3, 54 bytes

lambda s:fill(s,int(len(s)**.5))
from textwrap import*

Try it online!

In Python 3 the textwrap module can be used to perform this task.

answered Aug 27, 2019 at 8:14
\$\endgroup\$
0
\$\begingroup\$

Factor + math.unicode, (削除) 39 (削除ここまで) 38 bytes

[ dup length √ 1 /i group "\n"join ]

Try it online!

Explanation:

  • dup Duplicate the input.

    Stack: (e.g.) "Hi, world" "Hi, world"

  • length Get the length.

    Stack: "Hi, world" 9

  • Take the square root.

    Stack: "Hi, world" 3.0

  • 1 /i Convert to an integer. This is shorter than >integer.

    Stack: "Hi, world" 3

  • group Split a sequence into groups indicated by the integer on top of the data stack.

    Stack: { "Hi," " wo" "rld" }

  • "\n"join Join each string in a sequence into a newline-delimited string.

    Stack: "Hi,\n wo\nrld"

answered Jun 6, 2021 at 20:54
\$\endgroup\$
0
\$\begingroup\$

Canvas, 3 bytes

l√n

Try it here!

Ties Vyxal and uses pretty much the same approach but without those icky yucky awful terrible flags.

answered Jun 6, 2021 at 23:45
\$\endgroup\$
0
\$\begingroup\$

Brachylog, 7 bytes

ẹġṁcmẉm

Try it online!

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.

answered Jun 7, 2021 at 0:21
\$\endgroup\$
0
\$\begingroup\$

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
emanresu A
46.2k5 gold badges111 silver badges257 bronze badges
answered Sep 6, 2016 at 23:29
\$\endgroup\$
0
\$\begingroup\$

Thunno 2 N, 3 bytes

lƭ/

Attempt This Online!

Explanation

lƭ/ # Implicit input
l # Length of input
 ƭ # Square root
 / # Split into that
 # many pieces
 # Implicit output,
 # joined on newlines
answered Jun 10, 2023 at 17:10
\$\endgroup\$
1 2
3

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.