[1차] 비밀지도 - Python
2023. 10. 17. 11:04ㆍ공부/📝 프로그래머스
1. 풀이 코드
def solution(n, arr1, arr2):
answer = []
for map1, map2 in zip(arr1, arr2):
temp = f"{map1 | map2:016b}"
print(temp[-n:])
answer.append(temp[-n:].replace("1", "#").replace("0", " "))
return answer
# Test Cases
print(solution(5, [9, 20, 28, 18, 11], [30, 1, 21, 17, 28]))
print(solution(6, [46, 33, 33, 22, 31, 50], [27, 56, 19, 14, 14, 10]))
비트연산자 or을 통해 두 지도의 데이터를 합친 뒤 binary로 바꿔주었습니다.
2. 다른 사람 풀이 코드
def solution(n, *maps):
return [line(n, a | b) for a, b in zip(*maps)]
def line(n, x):
return ''.join(' #'[int(i)] for i in f'{x:016b}'[-n:])
def test_sample():
assert solution(5, [9, 20, 28, 18, 11], [30, 1, 21, 17, 28]) == [
'#####',
'# # #',
'### #',
'# ##',
'#####',
]
assert solution(6, [46, 33, 33, 22, 31, 50], [27, 56, 19, 14, 14, 10]) == [
'######',
'### #',
'## ##',
' #### ',
' #####',
'### # ',
]
def test_line():
assert line(5, 9) == ' # #'
assert line(5, 30) == '#### '
assert line(5, 9 | 30) == '#####'
테크니컬하네요. 특히나 assert를 사용해서 디버깅하는 모습에서 신뢰도가 높은 코드입니다.
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
'공부 > 📝 프로그래머스' 카테고리의 다른 글
푸드 파이트 대회 - Python (0) | 2023.10.17 |
---|---|
K번째수 - Python (0) | 2023.10.17 |
문자열 내 마음대로 정렬하기 - Python (0) | 2023.10.17 |
영어 끝말잇기 - Python (0) | 2023.10.16 |
숫자 문자열과 영단어 - Python (0) | 2023.10.16 |