본문 바로가기
PROGRAMMERS

[프로그래머스/파이썬] [3차] n진수 게임

by kode-daks 2025. 1. 21.

내 풀이

def solution(n, t, m, p):
    p -= 1 # 0부터 시작
    answer = ''
    
    # n진수로 변환하는 함수
    def to_n(x):
        over_nine = {10:"A", 11:"B", 12:"C", 13:"D", 14:"E", 15:"F"}
        n_num = ""
        
        # 가장 큰 자리수 확인
        max_i = 0
        while x >= n**(max_i+1):
            max_i += 1
		
        # 자리수 줄여가면서 변환
        while max_i >= 0:
            _n_num = x // n**max_i
            x -= _n_num * (n**max_i)
            if _n_num in over_nine: # 10 이상부터는 변환
                _n_num = over_nine[_n_num]
            n_num += str(_n_num)
            max_i -= 1
        return n_num
    
    # 모든 턴이 돌 때까지 문자열 준비
    i = 0
    _answer = ""
    while len(_answer) < t*m:
        _answer += str(to_n(i))
        i += 1
	
    # t가 될 때까지 튜브가 말해야 할 숫자 answer에 더함
    j = 0
    while len(answer) < t:
        answer += _answer[p+j*m]
        j += 1
    return answer

 

메모

quotient, remainder = divmod() : 몫과 나머지를 반환하는 파이썬 내장 함수