Draw some mountain peaks
Charcoal, 16 bytes
NλFλ«P↘⁻λι←↙¹‖T→
Try it online!
How?
Nλ
inputs the size of the largest mountain into λ
. Fλ«
runs a loop over values of ι
from 0
through λ-1
. (The closing »
is implied at the end of the program.)
Inside the loop, P↘⁻λι
calculates λ-ι
and draws, without moving the cursor afterward, a line of that length going southeast. Based on its direction, this line will consist of \
characters. ←
moves one step to the west, and ↙¹
draws a line of length 1 going southwest (made of /
). Finally, ‖T→
horizontally reflects the drawing, transforming characters as appropriate: \
becomes /
and /
becomes \
.
Adding the dump instruction D
at the beginning of the loop (try it) allows us to see the progression:
/\
/
/
/
/
/\
/\ \
/ \
/ \
/ \
/\
/ /\
/ /\ \
/ / \
/ / \
/\
/\ \
/ /\ \
/ /\ \ \
/ / \ \
/\
/ /\
/ /\ \
/ / /\ \
/ / /\ \ \
JavaScript (ES6), 75 bytes
for(n=prompt(s="/\\");n--;s=n%2?s+' \\':'/ '+s)console.log(" ".repeat(n)+s)
The full program is currently slightly shorter than the recursive function:
f=n=>n?" ".repeat(--n)+`/\\
`+f(n).replace(/\S.+/g,x=>n%2?x+" \\":"/ "+x):""
Python 2, 67 bytes
n=input()
s='/\\'
while n:n-=1;print' '*n+s;s=['/ '+s,s+' \\'][n%2]
Prints line by line, accumulating the string s
by alternately adding a slash to the left or right based on the current parity of n
. Prefixes with n
spaces.
An alternative way to update was the same length:
s=n%2*'/ '+s+~n%2*' \\'
s=['/ '+s,s+' \\'][n%2]
A recursive method was longer (70 bytes).
f=lambda n,s='/\\':n*'_'and' '*~-n+s+'\n'+f(n-1,[s+' \\','/ '+s][n%2])