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
atoz. - 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 code-golf. Shortest answer in bytes wins. Standard loopholes apply.
20 Answers 20
Charcoal, (削除) 25 (削除ここまで) (削除) 22 (削除ここまで) 21 bytes
≔÷Lθ4λ↙...θλ→✂θλ±λ↖✂θ±λ
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:
GH↙→→↖⊕÷Lθ4θ
Try it online! Link is to verbose version of code.
-
\$\begingroup\$ What do the
✂s do? \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年06月22日 09:19:38 +00:00Commented Jun 22, 2017 at 9:19 -
\$\begingroup\$ @EriktheOutgolfer That's the new Slice operator. \$\endgroup\$Neil– Neil2017年06月22日 09:52:24 +00:00Commented Jun 22, 2017 at 9:52
-
\$\begingroup\$ :| Oops meant to make PolygonHollow do this,
GH↙→→↖⊕÷Lθ4θwill work next time I push Charcoal \$\endgroup\$ASCII-only– ASCII-only2017年09月07日 08:26:18 +00:00Commented Sep 7, 2017 at 8:26
05AB1E, 23 bytes
ćsIg4÷GćsÁćsŠN·<ú«s}».C
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:
ć # extract head
sÁ # swap remaining string to top of stack and rotate right
ć # extract head
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), (削除) 119 (削除ここまで) (削除) 117 (削除ここまで) (削除) 108 (削除ここまで) 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() {
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--);
}
}
}
-
\$\begingroup\$ Pardon my ignorance, but what is the purpose of Action in your solution? Is it just less bytes than a void function? \$\endgroup\$confusedandamused– confusedandamused2017年06月22日 18:40:47 +00:00Commented Jun 22, 2017 at 18:40
-
1\$\begingroup\$ @confusedandamused I'm used to writing single function answers so didn't even consider putting the function normally, it will be shorter though. \$\endgroup\$LiefdeWen– LiefdeWen2017年06月22日 22:50:07 +00:00Commented Jun 22, 2017 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"]
-
\$\begingroup\$ We all know that
[[1]]can be replaced with#&@@. \$\endgroup\$user202729– user2027292017年06月21日 13:47:25 +00:00Commented Jun 21, 2017 at 13:47 -
1\$\begingroup\$ you are all so smart sweeties! \$\endgroup\$ZaMoC– ZaMoC2017年06月21日 13:52:16 +00:00Commented Jun 21, 2017 at 13:52
-
\$\begingroup\$ I meant codegolf.stackexchange.com/questions/12900/… . \$\endgroup\$user202729– user2027292017年06月21日 13:56:44 +00:00Commented Jun 21, 2017 at 13:56
-
\$\begingroup\$ When you find yourself doing
@(...), just do[...]instead. And I haven't tested but you can probably save another byte by givingColumna name (or maybe even toColumn[#,Alignment->Center]&to avoidq) and then by putting all the remaining variables into the first argument of the outerColumn(to save the surrounding parentheses). \$\endgroup\$Martin Ender– Martin Ender2017年06月21日 14:00:46 +00:00Commented Jun 21, 2017 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])
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.
-
\$\begingroup\$ Welcome to PPCG! You can learn from this solution. \$\endgroup\$Leaky Nun– Leaky Nun2017年06月22日 14:27:47 +00:00Commented Jun 22, 2017 at 14:27
-
\$\begingroup\$ A possible golf here is to declare
p=print, and then simply usepfor the threeprints you use. \$\endgroup\$FlipTack– FlipTack2017年12月22日 22:50:29 +00:00Commented Dec 22, 2017 at 22:50 -
\$\begingroup\$ Also, as the string length is guaranteed to always be divisible by four,
//(floor division) can be replaced with/. \$\endgroup\$FlipTack– FlipTack2017年12月22日 22:51:49 +00:00Commented Dec 22, 2017 at 22:51 -
\$\begingroup\$ By the way, the code you've linked to try online is not the same as the code in your answer. \$\endgroup\$FlipTack– FlipTack2017年12月22日 22:54:15 +00:00Commented Dec 22, 2017 at 22:54
GNU sed, (削除) 178 (削除ここまで) (削除) 158 (削除ここまで) 132 + 1 = 133 bytes
+1 byte for -r flag.
s/(.)(.*)(.)/ 1円\n2円;3円/
:
s/( *)(.\n.)(.*)(...);(.*)(.)/1円2円1円 6円\n3円;4円5円/m
t
:A
s/(.*\n)( *)(.*);/ 2円;1円2円3円/m
tA
s/. (.)$/1円/gm
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円\n2円;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円\n3円;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
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]
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
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.
-
1\$\begingroup\$ Can you shave off a byte by not doing integer division? Since
awill always be a multiple of 4.//->/\$\endgroup\$jacoblaw– jacoblaw2017年06月21日 17:01:18 +00:00Commented Jun 21, 2017 at 17:01 -
\$\begingroup\$ Thank you, I am surprised it is not throwing any errors even for [inputs with length not divisible by 4] [tio.run/… \$\endgroup\$0xffcourse– 0xffcourse2017年06月21日 17:11:02 +00:00Commented Jun 21, 2017 at 17:11
-
1\$\begingroup\$ That's because in Python 2, division is integer by default. That was canged in Python 3. \$\endgroup\$CalculatorFeline– CalculatorFeline2017年06月21日 17:57:17 +00:00Commented Jun 21, 2017 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
}
-
\$\begingroup\$ 163 bytes \$\endgroup\$ceilingcat– ceilingcat2020年08月31日 22:28:28 +00:00Commented Aug 31, 2020 at 22:28
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);
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
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>=7.1, 124 Bytes
for(;$i*2<$w=strlen($a=$argn)/2;$e=$a[-++$i],$s.=$s?" ":" ")echo str_pad("",$w/2-$i)."$a[$i]$s$e
";echo substr($a,$i,$w+1);
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
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, (削除) 213 (削除ここまで) (削除) 211 (削除ここまで) 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:
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:
" ".repeat(n) // Leading spaces
+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:
" ".repeat(n-i) // Leading spaces
+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
-
\$\begingroup\$ @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'svarinstead of that separated method andString. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2020年01月25日 09:23:29 +00:00Commented Jan 25, 2020 at 9:23
Vyxal C, 29 bytes
4/÷ḣ‟+^ṘYṘ2ẇṘḣð£ƛ\j\ðd+£;J^JJ
No canvas builtins! Because Vyxal doesn't have those. feature-request?
-9 thanks to Aaron Miller.
-
\$\begingroup\$ 29 bytes \$\endgroup\$Aaroneous Miller– Aaroneous Miller2021年07月17日 05:07:49 +00:00Commented Jul 17, 2021 at 5:07
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.