i=k=input()
while i:i-=1;print(" "*(k+~i)+"\\"+i*' / |\ '[i%2::2])[:k-~i]+"_/"[i>0:]
Experimente online!
Um dos truques de Erik me permitiu jogar 3 bytes! Economizou 3 bytes graças a Jonathan Allan.
Como isso funciona
Primeiro, isso obtém a entrada do STDIN e a atribui a duas variáveis separadas ie k. Então, enquanto a variável ié verdadeira, nós a diminuímos e geramos as seqüências de acordo; este é um atalho para fazer um loop da entrada - 1 até 0.
Gerando as cordas
Vou dividir isso em mais partes:
Primeiro, obter o espaçamento inicial é obtido com " "*(k+~i). Como ié mapeado através do intervalo (entrada 0) , devemos subtraí-lo de k(nossa entrada original armazenada com segurança), diminuir e repetir um espaço tantas vezes.
+"\\"- Adiciona o caractere "\"aos espaços acima.
' / |\ '[i%2::2]- Gera nossas duas strings, a saber , "/ \ "e " | "da seguinte maneira:
Se ifor ímpar, i% 2 é 1 , portanto, [i%2::2]retorna cada 2 caracteres de nossa cadeia maior, começando no índice 1 (indexado 0).
Se ifor par, i% 2 é 1 , portanto, o mecanismo acima faz o mesmo, exceto que inicia no índice 0 .
+~-i*- Repete-se a sequência gerada acima, seja "/ \ "ou " | ", I-1 vezes, e acrescenta-lo a outras cadeias. O benefício do operador bit a bit ( ~- Bitwise Complement, equivalente a i subtraído de -1 ) é que ele não requer parênteses nesse contexto.
[:k-~i]- Obtém todos os caracteres das seqüências de caracteres concatenadas acima até o índice k- ~ i = k - (-1 - i) = k + 1 + i .
+"_/"[i>0:]- Isso adiciona apenas "/"se i ≥ 1 , caso contrário ele é anexado _/.
Exemplo completo / detalhes de execução
Vamos pegar um exemplo de como as coisas funcionam para uma entrada 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.