Make text triangle waves
05AB1E, 13 12 bytes
1 byte saved thanks to Erik the Outgolfer
[¹ð.ø²Lû¨×€,
Try it online!
Run locally if you want the lines displayed 1 at a time.
Explanation
[ # start infinite loop
¹ð.ø # surround the first input by a space on each side
²Lû¨ # construct the range [1 ... n ... 2]
× # replace each number in the range by that many instances of the string
€ # for each string in the list
, # print it on a separate line
Haskell, 68 62 bytes
t#n=mapM putStrLn[[1..n-abs m]>>' ':t++" "|m<-[1-n..n-2]]>>t#n
Try it online! Example usage: "somestring" # 5
.
Edit: Saved 6 bytes thanks to @Ørjan Johansen!
Given for example n=4
, [1-n..n-2]
constructs the list [-3,-2,-1,0,1,2]
. For each element m
in this list, [1..n-abs m]
is a list of length n
minus the absolute value of m
, thus we get lists of length 4-3, 4-2, 4-1, 4-0, 4-1
and 4-2
, that is 1, 2, 3, 4, 3
and 2
. For each of this numbers >>' ':t++" "
concatenates as many times the given string t
with a space added to back and front.
This yields a list of strings with the first triangle. mapM putStrLn
prints each of the strings in the list on a new line, then the function #
calls itself again with the same arguments and starts over.
Java 7, 128 129 128 125 bytes
+1 byte: Missed the padding after each character
-1 byte: Changed k==1 to k<2
-3 byes thanks to Kevin Cruijssen
void a(String s,int i){s=" "+s;for(int j=-1,k=1,l;;k+=j){for(l=0;l<k;)System.out.print(s+(++l<k?" ":"\n"));j=k==i|k<2?-j:j;}}
First post here so let me know if I'm doing something wrong. Any golfing help is much appreciated.
Try it online! Note: this times out after 60 seconds and then prints the results, the actual code continually prints infinitely
Ungolfed
void a(String s,int i){
s= " " + s;
for(int j=-1, k=1, l;; k+=j){
for(l=0; l<k;)
System.out.print(s+ (++l < k ? " " : "\n"));
j = (k == i | k < 2) ? -j : j;
}
}
Prepends a space to the input string then starts an infinite loop. For each iteration it prints the string k times and then prints a new line. k is then incremented / decremented based on the sign of j