Hackerrank - Staircase Solution
1 min read

Hackerrank - Staircase Solution

Hackerrank - Staircase Solution

Consider a staircase of size :

   #
  ##
 ###
####

Observe that its base and height are both equal to , and the image is drawn using # symbols and spaces. The last line is not preceded by any spaces.

Write a program that prints a staircase of size .

Function Description

Complete the staircase function in the editor below. It should print a staircase as described above.

staircase has the following parameter(s):

  • n: an integer

Input Format

A single integer, n, denoting the size of the staircase.

Constraints

Output Format

Print a staircase of size  using # symbols and spaces.

Note: The last line must have  spaces in it.

Sample Input

6 

Sample Output

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

Explanation

The staircase is right-aligned, composed of # symbols and spaces, and has a height and width of n=6.

Solution in Python

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the staircase function below.
def staircase(n):
    for i in range(n):
        print(" "*(n-i-1)+"#"*(i+1))
if __name__ == '__main__':
    n = int(input())

    staircase(n)
How to programmatically exit a python program
Normally a python program will exit automatically when the program ends. But some situations may arise when we would want to exit the program on meeting a condition or maybe we want to exit our program after a certain period of time.
https://bit.ly/2U0ZQLt
Learn how to exit a python program in a if condition

Enjoying these posts? Subscribe for more


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×