[1차] 비밀지도

내가 제출한 답

def to_bin(arr, n):
    ret = [format(i, 'b') for i in arr]
    tmp = ''
    for idx in range(len(ret)):
        if len(ret[idx]) < n:
            ret[idx] = '0'*(n-len(ret[idx])) + ret[idx]
    return ret

def to_sharp(arr1,arr2):
    arr = []
    for a1, a2 in zip(arr1, arr2):
        a_len = len(a1)
        in_arr = ''
        for i in range(a_len):
            if a1[i] == '1' or a2[i] == '1':
                in_arr += '#'
            else:
                in_arr += ' '
        arr.append(in_arr)
    return arr

def solution(n, arr1, arr2):
    return to_sharp(to_bin(arr1, n), to_bin(arr2, n))

배운 코드 1 (정규표현식)

import re

def solution(n, arr1, arr2):
    answer = ["#"]*n
    for i in range(0, n):
        answer[i] = str(bin(arr1[i]|arr2[i]))[2:]
        answer[i] = re.sub('1', '#', '0'*(n-len(answer[i]))+answer[i])
        answer[i] = re.sub('0', ' ', answer[i])
    return answer

배운 코드 2 (파이썬 비트연산자)

def solution(n, arr1, arr2):
    answer = []
    for i,j in zip(arr1,arr2):
        a12 = str(bin(i|j)[2:])
        a12=a12.rjust(n,'0')
        a12=a12.replace('1','#')
        a12=a12.replace('0',' ')
        answer.append(a12)
    return answer

좋은 웹페이지 즐겨찾기