-
[프로그래머스] [1차] 비밀지도Engineering WIKI/Programmers 2022. 4. 11. 16:56
코딩테스트 연습 - [1차] 비밀지도
비밀지도 네오는 평소 프로도가 비상금을 숨겨놓는 장소를 알려줄 비밀지도를 손에 넣었다. 그런데 이 비밀지도는 숫자로 암호화되어 있어 위치를 확인하기 위해서는 암호를 해독해야 한다. 다
programmers.co.kr
내 풀이
from collections import deque def solution(n, arr1, arr2): answer = [] for i in range(n): result = "" bin_arr1 = deque(format(arr1[i], 'b')) bin_arr2 = deque(format(arr2[i], 'b')) if len(bin_arr1) < n: for j in range(n - len(bin_arr1)): bin_arr1.appendleft("0") if len(bin_arr2) < n: for j in range(n - len(bin_arr2)): bin_arr2.appendleft("0") for a, b in zip(bin_arr1, bin_arr2): if a == '1' or b == '1': result += "#" elif a == '0' and b == "0": result += " " answer.append(result) return answer
다른방법 1
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
다른 방법 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) == '#####'
'Engineering WIKI > Programmers' 카테고리의 다른 글
[프로그래머스] 나누어 떨어지는 숫자 배열 (0) 2022.04.12 [프로그래머스] 같은 숫자는 싫어 (0) 2022.04.11 [프로그래머스] 다트 게임 (0) 2022.04.11 [프로그래머스] 가운데 글자 가져오기 (0) 2022.04.11 [프로그래머스] 부족한 금액 계산하기 (0) 2022.04.07 [프로그래머스] 나머지가 1이 되는 수 찾기 (0) 2022.04.06 [프로그래머스] 최소 직사각형 (0) 2022.04.05 [프로그래머스] 두 개 뽑아서 더하기 (0) 2022.04.05