ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 역 피라미드 만들기
    코딩 테스트/Level 1 2021. 9. 8. 22:00
    반응형
    12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 
       11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 
          10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 
             9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 
               8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 
                 7 6 5 4 3 2 1 2 3 4 5 6 7 
                   6 5 4 3 2 1 2 3 4 5 6 
                     5 4 3 2 1 2 3 4 5 
                       4 3 2 1 2 3 4 
                         3 2 1 2 3 
                           2 1 2 
                             1

     

    생각의 흐름... 

    def solution(n: int):
        for i in range(n, 0, -1):
            for j in range(n - i):
                print(' ' * len(str(n + 1 - j)), end=' ')
            for j in range(i, 0, -1):
                print(j, end=' ')
            for j in range(2, i + 1):
                print(j, end=' ')
            print()
    
    
    solution(12)

     

    위는 너무 복잡한 것 같아... 
    좀 단순하게... 

    def solution(n: int):
        for i in range(n, 0, -1):
            for j in range(n, 0, -1):
                if i >= j:
                    print(j, end=' ')
                else:
                    print(' ' * len(str(j)), end=' ')
            for j in range(2, i + 1):
                print(j, end=' ')
            print()

     

    if 문을 줄여서... 

    def solution(n: int):
        for i in range(n, 0, -1):
            for j in range(n, 0, -1):
                print(j if i >= j else ' ' * len(str(j)), end=' ')
            for j in range(2, i + 1):
                print(j, end=' ')
            print()

     

    반응형
Designed by Tistory.