EunGyeongKim

[월간코드]3진법 뒤집기 본문

코딩테스트/programmers

[월간코드]3진법 뒤집기

EunGyeongKim 2022. 9. 20. 16:10

더 많은 코드 (https://github.com/EunGyeongKim/TIL)

 

GitHub - EunGyeongKim/TIL: Today I Learne

Today I Learne. Contribute to EunGyeongKim/TIL development by creating an account on GitHub.

github.com

문제 링크

 

프로그래머스

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

programmers.co.kr

1. 문제 설명

자연수 n이 매개변수로 주어집니다. n을 3진법 상에서 앞뒤로 뒤집은 후, 이를 다시 10진법으로 표현한 수를 return 하도록 solution 함수를 완성해주세요.


2. 제한사항

• n은 1 이상 100,000,000 이하인 자연수입니다.


3. 입출력 예

n result
45 7
125 229

4. 코드

import string

#진법 변환
tmp = string.digits+string.ascii_lowercase
def convert(num, base) :
    q, r = divmod(num, base)
    if q == 0 :
        return tmp[r] 
    else :
        return convert(q, base) + tmp[r]

def solution(n):
    answer = int(convert(n, 3)[::-1], 3)

    return answer
Comments