Staircase detail
This is a staircase of size n=4:
#
##
###
####
Its base and height are both equal to 4 . It is drawn using # symbols and spaces. The last line is not preceded by any spaces.
Write a program that prints a staircase of size n.
Function Description
Complete the staircase function in the editor below.
staircase has the following parameter(s):
- int n: an integer
Print a staircase as described above.
Input Format
A single integer,n , denoting the size of the staircase.
Constraints
0<=n<=100
Output Format
Print a staircase of size n using # symbols and spaces.
Note: The last line must have 0 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 ofn=6 .
Implementation
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'staircase' function below.
#
# The function accepts INTEGER n as parameter.
#
def staircase(n):
# Write your code here
for i in range(1, n + 1):
spaces = " " * (n - i)
hashes = "#" * i
print(spaces + hashes)
if __name__ == '__main__':
n = int(input().strip())
staircase(n)
In this solution:
- The outer loop runs for
niterations, wherenis the given size of the staircase. - In each iteration of the loop, the program performs constant time operations such as string multiplication and concatenation. These operations are not dependent on the size of the input
n.
Therefore, the overall time complexity of the solution is O(n), where n is the size of the staircase. The time complexity is linear with respect to the input size because the number of iterations directly scales with the input n, and the operations within each iteration are constant time.