본문 바로가기

알고리즘 문제풀이

Swea D3_11750.이진수1 Python 2진법

t = int(input())
dict_hexa = {
    '0':[0,0,0,0],
    '1':[0,0,0,1], # 1
    '2':[0,0,1,0], # 2
    '3':[0,0,1,1], # 3
    '4':[0,1,0,0], # 4
    '5':[0,1,0,1], # 5
    '6':[0,1,1,0], # 6
    '7':[0,1,1,1], # 7
    '8':[1,0,0,0], # 8
    '9':[1,0,0,1], # 9
    'A':[1,0,1,0], # 10(A)
    'B':[1,0,1,1], # 11(B)
    'C':[1,1,0,0], # 12(C)
    'D':[1,1,0,1], # 13(D)
    'E':[1,1,1,0], # 14(E)
    'F':[1,1,1,1],
}
for tc in range(1,t+1):
    n, n16 = map(str,input().split())
    # print(n16)
    new_str = []
    for item in n16:
        new_str += dict_hexa[item]
    # print(new_str)
    print(f"#{tc} {''.join(map(str,new_str))}")

딕셔너리의 value에 리스트로 두지말고 str로 두는게 나았을것 같다.

hexa = {
    '0':'0000',    '1':'0001',    '2':'0010',    '3':'0011',    '4':'0100',    '5':'0101',
    '6':'0110',    '7':'0111',    '8':'1000',    '9':'1001',    'A':'1010',    'B':'1011',
    'C':'1100',    'D':'1101',    'E':'1110',    'F':'1111'
}
t = int(input())
for tc in range(1,t+1):
    n, n16 = map(str,input().split())
    new_str = ''
    for i in n16:
        new_str += hexa[i]
    print(new_str)