본문 바로가기
Algorithm/프로그래머스

[프로그래머스] - n진수 게임 (파이썬)

by 2HS 2022. 2. 6.
alpha = {10: 'A', 11: 'B', 12: 'C', 13: 'D', 14: 'E', 15: 'F'}


def solution(n, t, m, p):
    all = ''
    count = 0
    needs = p
    for _ in range(t-1):
        needs += m
    while True:
        if len(all) > needs:
            break
        all_temp = ''
        temp = count
        while True:
            k = temp % n
            if k in alpha:
                k = alpha[k]
            all_temp = str(k) + all_temp
            if temp // n != 0:
                temp = temp // n
            else:
                break
        all += all_temp
        count += 1

    answer = ''
    for i in range(p-1, (t-1)*m+p, m):
        answer += str(all[i])

    return answer

 

2018 카카오 블라인드 3차 - 1번

1. needs (필요한 배열의 총 길이)만큼 총배열(all_temp)를 만든다

    - temp를 0부터 순서대로 해당 진법(n)으로 만들어준다.

    - 16진수 일 경우 딕셔너리를 이용해서 변경

2. answer에 p부터 m씩증가한 수 t개를 넣는다

3. answer출력

 

최대한 빨리 풀려고 하다보니 난잡해졌다