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.
\$\endgroup\$
7
  • \$\begingroup\$ Can we assume that the input will never have new lines? \$\endgroup\$
    – bren
    Commented Aug 6, 2016 at 2:13
  • \$\begingroup\$ @MayorMonty yep. \$\endgroup\$
    – acrolith
    Commented Aug 6, 2016 at 2:20
  • 2
    \$\begingroup\$ Can we output array of strings instead? \$\endgroup\$
    – Leaky Nun
    Commented Aug 6, 2016 at 5:10
  • \$\begingroup\$ @LeakyNun no 15 chars \$\endgroup\$
    – acrolith
    Commented Aug 6, 2016 at 16:58
  • 2
    \$\begingroup\$ May we print with a trailing newline? \$\endgroup\$
    – Giuseppe
    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.

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

\$\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.

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

Tcl, 100 98 65 bytes

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

Try it online!

\$\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
\$\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.

\$\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"

\$\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.

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

Brachylog, 7 bytes

ẹġṁcᵐẉᵐ

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
   cᵐ    Concatenate each row back into a string
     ẉᵐ  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.

\$\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
\$\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
\$\endgroup\$
1 2
3

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.