39
3
Write a program or function that takes in a string guaranteed to only contain printable ASCII characters except for space, and to be a positive triangular number (1, 3, 6, 10, 15, ...) in length.
Print or return the same string, but shaped into a triangle using spaces. Some examples will best show what I mean:
If the input is R
then the output will be
R
If the input is cat
then the output will be
c
a t
If the input is monk3y
then the output will be
m
o n
k 3 y
If the input is meanIngfu1
then the output will be
m
e a
n I n
g f u 1
If the input is ^/\/|\/[]\
then the output will be
^
/ \
/ | \
/ [ ] \
If the input is
Thisrunofcharactersismeanttohavealengththatcanbeexpressedasatriangularnumber.Diditwork?Youtellme,Ican'tcountverywell,ok?
then the output will be
T
h i
s r u
n o f c
h a r a c
t e r s i s
m e a n t t o
h a v e a l e n
g t h t h a t c a
n b e e x p r e s s
e d a s a t r i a n g
u l a r n u m b e r . D
i d i t w o r k ? Y o u t
e l l m e , I c a n ' t c o
u n t v e r y w e l l , o k ?
Basically, newlines are inserted between the substrings of triangular length, spaces are added between all characters, and each line is indented with spaces to fit the triangle shape.
A single trailing newline and lines with trailing spaces are optionally allowed, but otherwise your output should exactly match these examples. The last line of the triangle should not have leading spaces.
The shortest code in bytes wins.
Is there an absolute max the length of the string can be? – geokavel – 2015-11-30T06:33:49.820
@geokavel It should work for any length of string your language can normally handle. – Calvin's Hobbies – 2015-11-30T06:41:52.053
11Here's a Christmas tree for anyone who hasn't put theirs' up yet. *//|/|o/|o|/o|o|/||o|o/o|||o|/o||o|||/||o|||o|/|o|||o||o\ – Timmy – 2015-11-30T20:30:36.383
potentially related – JohnE – 2015-12-02T00:13:04.730