Print a triangle

 

Print a triangle

Question: you need to print a triangle like this:

     #
    ##
   ###
  ####
 #####
######

Ideas:

  1. Think of this like a square

-----#
----##
---###
--####
-#####
######
  1. we need to print the left triangle first

for i in range(0,n):
 for j in range(0,n-i-1):
   print("-",end=" ")
 print("")
  1. now we can print the right triangle easier

for i in range(0,n):
 for j in range(0,n-i-1):
   print("-",end="")
 for j in range(0,i+1):
   print("*",end="")
 print("")
  1. change "-" to ""

def staircase(n):
   # Write your code here
   for i in range(0, n):
       for j in range(0, n-i-1):
           print("-",end="")
           # print("",end = " ")
       for j in range(0, i+1):
           print("#",end="")
       # if i<n:
       print('')

Reference: https://www.hackerrank.com/challenges/staircase/problem?isFullScreen=true&h_r=next-challenge&h_v=zen


Comments