i=k=input()
while i:i-=1;print(" "*(k+~i)+"\\"+i*' / |\ '[i%2::2])[:k-~i]+"_/"[i>0:]
Try it online!
One of Erik’s tricks allowed me to golf 3 bytes! Saved 3 bytes thanks to Jonathan Allan.
How this works
First off, this gets input from STDIN and assigns it to two separate variables i
and k
. Then, while the variable i
is truthy, we decrement it and generate the strings accordingly; this is a shorthand for looping from the input - 1 all the way down to 0.
Generating the Strings
I'll split this into more parts:
First off, getting the leading spacing is achieved with " "*(k+~i)
. Since i
is mapped through the range (input, 0], we must subtract it from k
(our safely stored original input), decrement and repeat a space that many times.
+"\\"
- Adds the character "\"
to the spaces above.
' / |\ '[i%2::2]
- Generates our two strings, namely "/ \ "
and " | "
, in the following manner:
If i
is odd, i % 2 is 1, thus [i%2::2]
returns each 2 characters of our larger string, starting at index 1 (0-indexed).
If i
is even, i % 2 is 1, thus the mechanism above does the same except it starts at index 0.
+~-i*
- Repeats the string generated above, either "/ \ "
or " | "
, i-1 times, and appends it to the other strings. The benefit of the bitwise operator (~
- Bitwise Complement, Equivalent to i subtracted from -1) is that it doesn't require parenthesis in this context.
[:k-~i]
- Gets all the characters of the strings concatenated above until index k-~i = k - (-1 - i) = k + 1 + i.
+"_/"[i>0:]
- This only adds "/"
if i ≥ 1, else it appends _/
.
Full example / execution details
Let's grab an example of how things work for an input of 4:
i=k=input() # i and k are assigned to 4.
while i: # Starts the loop. The initial value of i is 4.
i-=1; # Decrement i. i is now 3.
" "*(k+~i) # A space repeated k - 1 - i = 4 - 1 - 3 = 0 times.
+"\\" # Plus the character "\". CS (Current string): "\".
' / |\ '[i%2::2] # The string ' / |\ '[3%2::2] = ' / |\ '[1::2] = "/ \ ".
i* # ^ repeated i = 3 times: "/ \ / \ / \ ".
+ # And concatenate. CS: "\/ \ / \ / \ "
[:k-~i] # Get the characters of ^ up to index k + 1 + i = 4 + 1 + 3 = 8.
# CS: "\/ \ / \".
+"_/"[i>0:] # Append "_/"[i>0:] = "_/"[3>0:] = "_/"[1:] = "/".
# CS: "\/ \ / \/".
print # Output the result "\/ \ / \/".
while i: # i is truthy (> 0), thus we loop again.
i-=1; # Decrement i. i becomes 2.
" "*(k+~i) # " " repeated 4 - 2 - 1 = 1 time.
+"\\" # Plus "\". CS: " \".
' / |\ '[i%2::2] # ' / |\ '[2%2::2] = ' / |\ '[::2] = " | ".
+i* # Repeat i = 2 times and append: " | ". CS: " \ | |".
[:k-~i] # CS up until k + 1 + i = 4 + 2 + 1 = 7. CS: " \ | ".
+"_/"[i>0:] # Append "/". CS: " \ | /".
print # Outputs the CS: " \ | /".
while i: # i is truthy (> 0), thus we loop again.
i-=1; # Decrement i. i is now 1.
" "*(k+~i) # " " repeated 4 - 1 - 1 = 2 times.
+"\\" # Plus "\". CS: " \".
' / |\ '[i%2::2] # ' / |\ '[2%2::2] = ' / |\ '[::2] = "/ \ ".
+i* # Repeat i = 1 time and append: "/ \ ". CS: " \/ \ ".
[:k-~i] # CS up until k + i + 1 = 4 + 2 = 6. CS: " \/ \".
+"_/"[i>0:] # Append "/". CS: " \/ \/".
print # Outputs the CS: " \/ \/".
while i: # i is truthy (> 0), thus we loop again.
i-=1; # Decrement i. i is now 0.
" "*(k+~i) # " " repeated 4 - 1 - 0 = 3 times.
+"\\" # Plus "\". CS: " \".
' / |\ '[i%2::2] # ' / |\ '[1%2::2] = ' / |\ '[1::2] = " | ".
+i* # Repeat i = 0 times and append: " \". CS: " \".
[:k-~i] # CS up until k + i + 1 = 4 + 0 + 1 = 5. CS: " \".
+"_/"[i>0:] # Append "_/" (because i > 0 is False since i == 0). CS: " \_/".
print # Outputs the CS: " \_/".
while i: # i == 0, hence the condition is falsy and the loop ends.
# Program terminates.