본문 바로가기

알고리즘 공부/프로그래머스

프로그래머스 124 나라의 숫자 with Python

문제

https://school.programmers.co.kr/learn/courses/30/lessons/12899

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

해결한 방법

def solution(n):
    li =[]
    while(n>=3):
        li.append(n%3)
        n = n//3
    li.append(n)
    li.reverse()
    
    while(0 in li):
        point = li.index(0)
        li[point] = 3
        li[point-1] -= 1
        if li[0] == 0:
            li.remove(0)

    answer = ''
    for i in li:
        if i == 3:
            answer += '4'
        else:
            answer += str(i)

    return answer