프로그래머스 [1차] 비밀지도 (python)

먼지감자·2021년 6월 22일
0

코딩테스트

목록 보기
27/37

문제

https://programmers.co.kr/learn/courses/30/lessons/17681

코드

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

설명

int | int -> 두 수의 비트 or 연산
bin(int) -> 2진수로 변환
[2:] 이진수로 변환하면 앞에 '0b'가 붙기 때문에 앞 두개는 제외
길이를 맞추기 위해 앞에 0을 채움

str 앞에 0 을 채우는 법

  • str.zfill(n) : n 길이가 될때까지 앞에 0을 붙임
  • str.rjust(n, '0') : n 길이가 될때까지 인자로 넘긴 문자를 앞에 붙임
profile
ML/AI Engineer

0개의 댓글