반응형
programmers.co.kr/learn/courses/30/lessons/68935
코딩테스트 연습 - 3진법 뒤집기
자연수 n이 매개변수로 주어집니다. n을 3진법 상에서 앞뒤로 뒤집은 후, 이를 다시 10진법으로 표현한 수를 return 하도록 solution 함수를 완성해주세요. 제한사항 n은 1 이상 100,000,000 이하인 자연수
programmers.co.kr

단순구현 문제.
진법 변환하는 원리만 알면 풀 수 있다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solution(n): | |
answer = [] | |
while True: | |
n, rest = divmod(n, 3) | |
answer.append(rest) | |
if n == 0: | |
break | |
return sum([i * 3**idx for idx, i in enumerate(reversed(answer))]) |
반응형
'프로그래밍 > 코딩테스트 문제풀이' 카테고리의 다른 글
[Python] LeetCode 238. Product of Array Except self (0) | 2020.10.19 |
---|---|
[Python] 프로그래머스. 쿼드압축 후 개수세기 (1) | 2020.10.14 |
[Python] LeetCode 15. 3Sum (0) | 2020.10.08 |
[Python] 프로그래머스. 줄 서는 방법 (Level 3) (0) | 2020.10.06 |
[Python] 프로그래머스. JadenCase 문자열 (Level 2) (1) | 2020.10.05 |