# Fold a string into a triangle

Given a string whose length is divisible by 4, make a triangle as demonstrate below.

If the string is abcdefghijkl, then the triangle would be:

   a
b l
c   k
defghij


If the string is iamastringwithalengthdivisiblebyfour, then the triangle would be:

         i
a r
m   u
a     o
s       f
t         y
r           b
i             e
n               l
gwithalengthdivisib


If the string is thisrepresentationisnotatriangle, then the triangle would be:

        t
h e
i   l
s     g
r       n
e         a
p           i
r             r
esentationisnotat


## Notes

• The string will only consist of characters from a to z.
• Leading/Trailing whitespaces and newlines are allowed as long as the shape is not broken.
• A list of strings as output is allowed.

This is . Shortest answer in bytes wins. Standard loopholes apply.

# Charcoal, 2522 21 bytes

≔÷Ｌθ⁴λ↙…θλ→✂θλ±λ↖✂θ±λ


Try it online! Link is to verbose version of code. Simply slices the string into three parts and prints them in the appropriate directions. Edit: Saved 3 bytes by using integer division and slicing. Saved a further byte by using CycleChop instead of Slice for the head of the string. Edit: Charcoal now supports drawing arbitrary text along the edge of a polygon, simplifying the code to 12 bytes:

ＧＨ↙→→↖⊕÷Ｌθ⁴θ


Try it online! Link is to verbose version of code.

• What do the ✂s do? – Erik the Outgolfer Jun 22 '17 at 9:19
• @EriktheOutgolfer That's the new Slice operator. – Neil Jun 22 '17 at 9:52
• :| Oops meant to make PolygonHollow do this, ＧＨ↙→→↖⊕÷Ｌθ⁴θ will work next time I push Charcoal – ASCII-only Sep 7 '17 at 8:26

# 05AB1E, 23 bytes

ćsIg4÷GćsÁćsŠN·<ú«s}».C


Try it online!

Explanation

ć                        # extract head of input
s                       # swap the remaining string to top of stack
Ig4÷G                  # for N in [1...len(input)/4-1] do:
sÁ               # swap remaining string to top of stack and rotate right
sŠ            # reorder stack as tail, head, remaining
N·<ú        # prepend N-1 spaces to tail
«s      # concatenate with head and swap remaining string to top
}     # end loop
».C  # join by newlines and center


## JavaScript (ES6), 119117108 105 bytes

s=>(l=s.length/4,S=' ',g=([c,...s],p)=>S.repeat(l)+c+(l--?p+s.pop()+
+g(s,p?p+S+S:S):s.join))(s+S,'')


### Formatted and commented

s => (                            // given the input string s:
l = s.length / 4,               // l = length of side edge - 1
S = ' ',                        // S = space (defining S costs 6 bytes but saves 7)
g = (                           // g = recursive function which takes:
[c,                        //   - c = next character
...s],                 //   - s = array of remaining characters
p) =>           //   - p = middle padding string
S.repeat(l) + c + (           // append left padding + left character
l-- ?                       // if side edges are not complete:
p + s.pop() + '\n' +      //   append middle padding + right character + Line Feed
g(s, p ? p + S + S : S)   //   and do a recursive call with updated middle padding
:                           // else:
s.join                  //   append all remaining characters and stop recursion
)                             //   (this is the bottom edge)
)(s + S, '')                    // initial call to g()


### Test cases

let f =

s=>(l=s.length/4,S=' ',g=([c,...s],p)=>S.repeat(l)+c+(l--?p+s.pop()+
+g(s,p?p+S+S:S):s.join))(s+S,'')

console.log(f('abcdefghijkl'))
console.log(f('iamastringwithalengthdivisiblebyfour'))
console.log(f('thisrepresentationisnotatriangle'))

# C#, 260 bytes

namespace System{using static Console;class P{static void Main(){var d=ReadLine();int e=d.Length/4,x=e,y=0,g=0,i=0;Action<int,int>a=(p,q)=>{SetCursorPosition(p,q);Write(d[g++]);};for(;i<e;i++)a(x--,y++);for(i=0;i<e*2;i++)a(x++,y);for(i=0;i<e;i++)a(x--,y--);}}}


Really wanted to use SetCursorPosition.

Ungolfed:

namespace System {
using static Console;

class P {
static void Main() {
int e = d.Length / 4, x = e, y = 0, g = 0, i = 0;
Action<int, int> a = (p, q) => { SetCursorPosition(p, q); Write(d[g++]); };
for (; i < e; i++)
a(x--, y++);
for (i = 0; i < e * 2; i++)
a(x++, y);
for (i = 0; i < e; i++)
a(x--, y--);
}
}
}

• Pardon my ignorance, but what is the purpose of Action in your solution? Is it just less bytes than a void function? – confusedandamused Jun 22 '17 at 18:40
• @confusedandamused I'm used to writing single function answers so didn't even consider putting the function normally, it will be shorter though. – LiefdeWen Jun 22 '17 at 22:50

# Mathematica, 164 bytes

(b=Length[c=Characters@#];k=Column[#,Alignment->Center]&;T=Table;k@{#&@@c,k@T[""<>{c[[i+2]],T[" ",2i+1],c[[-i-1]]},{i,0,(a=b/4)-2}],""<>T[c[[i]],{i,a+1,b/2+1+a}]})&


input

["iamastringwithalengthdivisiblebyfour"]

• We all know that [[1]] can be replaced with #&@@. – user202729 Jun 21 '17 at 13:47
• you are all so smart sweeties! – J42161217 Jun 21 '17 at 13:52
• – user202729 Jun 21 '17 at 13:56
• When you find yourself doing @(...), just do [...] instead. And I haven't tested but you can probably save another byte by giving Column a name (or maybe even to Column[#,Alignment->Center]& to avoid q) and then by putting all the remaining variables into the first argument of the outer Column (to save the surrounding parentheses). – Martin Ender Jun 21 '17 at 14:00

# Python 3, 120 bytes

First golf, figured I might as well learn some Python along the way.

a=input()
l=len(a)//4
print(l*" "+a[0])
for i in range(1,l):print((l-i)*" "+a[i]+(2*i-1)*" "+a[4*l-i])
print(a[l:3*l+1])


Try it online!

Explanation:

The first character is printed by itself after len(a)//4 spaces, then the first and last i-th characters starting from the second are printed, separated by 2*i - 1 spaces.

Finally, the remaining substring is printed.

• Welcome to PPCG! You can learn from this solution. – Leaky Nun Jun 22 '17 at 14:27
• A possible golf here is to declare p=print, and then simply use p for the three prints you use. – FlipTack Dec 22 '17 at 22:50
• Also, as the string length is guaranteed to always be divisible by four, // (floor division) can be replaced with /. – FlipTack Dec 22 '17 at 22:51
• By the way, the code you've linked to try online is not the same as the code in your answer. – FlipTack Dec 22 '17 at 22:54

# GNU sed, 178158 132 + 1 = 133 bytes

+1 byte for -r flag.

s/(.)(.*)(.)/ \1\n\2;\3/
:
s/( *)(.\n.)(.*)(...);(.*)(.)/\1\2\1  \6\n\3;\4\5/m
t
:A
s/(.*\n)( *)(.*);/ \2;\1\2\3/m
tA
s/. (.)$/\1/gm  Try it online! ## Explanation In previous revisions I used a lot of bytes dealing with math, special cases, and cleanup, even though intuitively I was sure they could be avoided. I've since managed to do so, mostly. Suppose we have the input abcdEFGHIJKLMnop. The letters EFGHIJKLM will be the bottom of the triangle, so I've capitalized them as a visual aid. First we prepare the input by putting the first character on its own line (preceded by a space) and inserting a cursor (;) before the last character: s/(.)(.*)(.)/ \1\n\2;\3/  Now we have:  a bcdEFGHIJKLMno;p  Now, in a loop, we're going to do a few things to the last line: 1. Copy the spaces from the previous line and insert them after the first character, plus two; 2. Move the last character to right after the spaces, followed by a newline; and 3. Move the cursor three characters to the left. : s/( *)(.\n.)(.*)(...);(.*)(.)/\1\2\1 \6\n\3;\4\5/m t  Here's the result of each iteration:  a b p cdEFGHIJKL;Mno a b p c o dEFGHI;JKLMn a b p c o d n EF;GHIJKLM  You can see the pyramid begin to take shape. You can also see what the cursor was for: In each iteration it moved left three characters, and when there are no longer three characters to its left, it breaks the loop, which happens to be just when we've reached the "bottom" of the pyramid. Now we're going to do a similar operation but in reverse. In a loop, we'll copy the spaces from the beginning of the line with the cursor to the beginning of the preceding line, plus one, in the process moving the cursor up to that line. :A s/(.*\n)( *)(.*);/ \2;\1\2\3/m tA  Here are a couple iterations and the end result:  a b p c o ;d n EFGHIJKLM a b p ;c o d n EFGHIJKLM ... ; a b p c o d n EFGHIJKLM  We're all done now, except for some extra characters: A ; and extra space on the first line, and two spaces in the "middle" of the pyramid on the next three lines. A simple substitution gets rid of them: s/. (.)$/\1/gm


All done!

    a
b p
c   o
d     n
EFGHIJKLM


# Ruby, 106 bytes

i=-1
s= ~/$/ sub /./,"#{' '*l=s/4}\\0 " (l-1).times{sub /^(\w)(.*)(.)/,"#{' '*l-=1}\\1#{' '*i+=2}\\3 \\2"}  Try it online! # Python 2, 100 97 96 bytes • Jacoblaw saved 1 byte: integer division is unnecessary a=input()+" " k=j=len(a)/4 while j:print j*" "+a[0]+(2*(k-j)-1)*" "+a[-1];a=a[1:-1];j-=1 print a  Try it online! ### Explanation: One smart thing I've done here is padded the input with a space at the end, such that the first character pairs with it and this can be pushed into the loop (and since trailing whitespaces are allowed) abcdefghijkl[space] To print [0] [-1] Output=>[spaces]a[another_calculated_spaces(=0 here)][space] Strip at both ends(a[1:-1]) bcdefghijkl To print [0] [-1] Output=>[spaces]b[another_calculated_spaces]l Strip at both ends(a[1:-1]) and so on.  The number of loops to follow is associated with len(word)//4. In the final step, the whole remaining string is printed(this forms the base of the triangle). The spaces follow a simple pattern; the first set of spaces go-on decreasing by 1, while second set of spaces go on increasing by 2. • Can you shave off a byte by not doing integer division? Since a will always be a multiple of 4. // -> / – jacoblaw Jun 21 '17 at 17:01 • Thank you, I am surprised it is not throwing any errors even for [inputs with length not divisible by 4] [tio.run/… – officialaimm Jun 21 '17 at 17:11 • That's because in Python 2, division is integer by default. That was canged in Python 3. – CalculatorFeline Jun 21 '17 at 17:57 # C 225 bytes p(c){putchar(c);}S(n){while(n--)p(' ');}main(int c,char**v){int i= strlen(v[1]),n=i/4,r;char*s=v[1],*e=&s[i-1];S(n);p(*s++);p('\n');for (r=1;r<n;r++){S(n-r);p(*s++);S(2*r-1);p(*e--);p('\n');}e++;while (s!=e)p(*s++);p('\n');}  explained p(c){putchar(c);} // p is alias for putchar S(n){while(n--)p(' ');} // S prints n spaces main(int c,char**v){ int i= strlen(v[1]), // counter n=i/4, // num rows in figure - 1 r; // current row char*s=v[1], // start char *e=&s[i-1]; // end char S(n);p(*s++);p('\n');// print first row for (r=1;r<n;r++){ S(n-r);p(*s++);S(2*r-1);p(*e--);p('\n'); // print middle rows } e++;while (s!=e)p(*s++);p('\n'); // print last row }  # C#, 172 bytes int i=0,n=s.Length;var p="";p=new string(' ',n/4)+s[i]+"\r\n";for(i=1;i<n/4;i++){p+=new string(' ',n/4-i)+s[i]+new string(' ',i*2-1)+s[n-i]+"\r\n";}p+=s.Substring(i,n/2+1);  Try it online! # Octave, 87 bytes @(s,x=(n=nnz(s))/4)[[' ';flip(diag(s(1:x))')]' [' ';diag(s(n:-1:n-x+2))];s(x+1:n-x+1)];  *In a windows machine the above code produces the correct result however in tio I added some code to correct it. Explanation: [' ';flip(diag(s(1:x))')]' %left side [' ';diag(s(n:-1:n-x+2))] %right side s(x+1:n-x+1) %bottom side  Try it online! # Haskell, 136 bytes i#x=x<$[1..i]
f s|let h=div l 4;l=length s=unlines$[(h-i)#' '++(s!!i):(2*i-1)#' '++[(s++" ")!!(l-i)]|i<-[0..h-1]]++[drop h$take(l-h+1)s]


Try it online!

# PHP>=7.1, 122 Bytes

for(;$i*2<$w=strlen($a=$argn)/2;$e=$a[-++$i])echo str_pad(str_pad($a[$i],$i*2).$e,$w+1," ",2),"
";echo substr($a,$i,$w+1);  PHP Sandbox Online # AWK, 129 bytes {n=split($0,a,"")
printf"%"(w=n/4+1)"s\n",a[++i]
for(;++i<w;)printf"%"(w-i+1)"s%"2*i-2"s\n",a[i],a[n-i+2]
$0=substr($0,i,i+w-1)}1


Try it online!

I should think this could be golfed a bit more, just not seeing it.

# Retina, 99 bytes

^(.)(?=(....)+)
$#2$*  $1¶$#2$* ( ( *).)(.*)(.)$
$1$4¶$2$3
+(( +).¶ ( *).)(.*)(.)1$2$5¶$3$4


Try it online! Explanation: The first two stages generate the first two lines, but after that no special-casing is necessary and each subsequent line can be generated automatically:

thisrepresentationisnotatriangle

t
hisrepresentationisnotatriangle

t
h e
isrepresentationisnotatriangl

t
h e
i   l
srepresentationisnotatriang

...

t
h e
i   l
s     g
r       n
e         a
p           i
r             r
esentationisnotat


# Java 11, 213211 178 bytes

s->{int n=s.length()/4,i=1;var r=" ".repeat(n)+s.charAt(0)+"\n";for(;i<n;r+=" ".repeat(n-i)+s.charAt(i)+" ".repeat(i*2-1)+s.charAt(n*4-i++)+"\n");return r+s.substring(i,n*2-~i);}


-2 bytes thanks to @ceilingcat.

Explanation:

Try it online.

s->{                               // Method with String parameter and String return-type
int n=s.length()/4,              //  The length of the input divided by 4
i=1;                         //  And an index-integer, starting at 1
var r=                           //  Result-String which starts as:
+s.charAt(0)               //   + the first character
+"\n";                     //   + a new-line
for(;i<n;                        //  Loop i in the range [1, n):
r+=                          //   And append the result-String with:
+s.charAt(i)              //    + the character of the left diagonal line
+" ".repeat(i*2-1)        //    + center spaces
+s.charAt(n*4-i++)        //    + the character of the right diagonal line
+"\n");                   //    + a new-line
return r                         //  Return the result-String
+s.substring(i,n*2+i+1);} //   + the bottom part of the triangle

• @ceilingcat Thanks, pretty stupid mistake, haha. I've been able to golf it some more by using Java 11's " ".repeat(n) and Java 10's var instead of that separated method and String. – Kevin Cruijssen Jan 25 '20 at 9:23

# Perl 5, 76 bytes

74 bytes of code +2 for -F

say$"x($l=@F/4),shift@F;say$"x$l,shift@F,$"x($#i+=2),pop@F while--\$l;say@F


Try it online!

# 05AB1E, 14 bytes

g4/>x<‚ĆI527SΛ


Try it online (no test suite since there isn't a way to clean the Canvas).

Explanation:

g         # Take the length of the (implicit) input
#  i.e. "thisrepresentationisnotatriangle" → 32
4/       # Divide it by 4
#  i.e. 32 → 8
>      # Increase it by 1
#  i.e. 8 → 9
x     # Double it (without popping the value itself)
#  i.e. 9 → 9 and 18
<    # Decrease this doubled value by 1
#  i.e. 18 → 17
‚   # Pair them together: [length/4+1, (length/4+1)/2-1]
#  i.e. 9 and 17 → [9,17]
Ć  # Enclose it, appending its head to itself: [l/4+1, (l/4+1)/2-1, l/4+1]
#   i.e. [9,17] → [9,17,9]
I         # Push the input-string again
527S      # Push 527 as digit-list: [5,2,7]
Λ         # Use the Canvas builtin with these three arguments
# (after which the result is output implicitly)


Canvas explanation:

The Canvas builtin takes three arguments:
1. The lengths of the lines to 'draw', which in this case is the list $$\[\frac{l}{4}+1, \frac{\left(\frac{l}{4}+1\right)}{2}-1, \frac{l}{4}+1]\$$.
2. The string to 'draw', which in this case is the input. 3. The directions, which in this case is [5,2,7]. These will be the directions [↙,→,↖].

With these arguments, it will draw as follows (let's take the string "thisrepresentationisnotatriangle" as example again):

9 characters ("thisrepre") in direction 5 (↙)
t
h
i
s
r
e
p
r
e

17-1 characters ("sentationisnotat") in direction 2 (→):
t
h
i
s
r
e
p
r
esentationisnotatr

9-1 characters ("riangle") in direction 7 (↖):
(NOTE: "riangle" are only 7 characters instead of 8, since the string isn't any longer,
which won't cause problems and is actually convenient for our desired output)
t
h e
i   l
s     g
r       n
e         a
p           i
r             r
esentationisnotat
`

See this 05AB1E tip of mine for a more in-depth explanation of the Canvas builtin.