규도자 개발 블로그

[해커랭크(Hackerrank)/Problem Solving/파이썬3(python3)] Staircase 본문

알고리즘/풀이

[해커랭크(Hackerrank)/Problem Solving/파이썬3(python3)] Staircase

규도자 (gyudoza) 2019. 3. 9. 15:27

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, , 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 .

풀이

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the staircase function below.
def staircase(n):
    for i in range(1, n + 1):
        print(' ' * int(n - i), end='')
        print('#' * i)



if __name__ == '__main__':
    n = int(input())

    staircase(n)

설명

습관처럼 #이 아니라 *을 찍었다가 한번 틀렸던 문제이다. 별찍기가 아니라 #찍기라는 사실을 잊지 말자. 폰트차이때문에 내 블로그에선 위 #으로 쌓인 탑이 이상한 모양이지만 흔한 공백 & 문자 채우기 문제이다. 맨 윗칸은 공백이 5칸, # 1칸, 그 아래는 공백이 4칸, # 2칸 등으로 진행하면 된다.


Comments