기억은 짧고 기록은 길다
[프로그래머스/Programmers] [1차] 비밀지도 - Python 본문
Link
코딩테스트 연습 - [1차] 비밀지도
비밀지도 네오는 평소 프로도가 비상금을 숨겨놓는 장소를 알려줄 비밀지도를 손에 넣었다. 그런데 이 비밀지도는 숫자로 암호화되어 있어 위치를 확인하기 위해서는 암호를 해독해야 한다. 다
programmers.co.kr
Solution
정말 Other Solution의 풀이를 보고 감탄을 할 수 밖에 없었다.
시간복잡도도 시간복잡도지만 bin(i | j) 연산을 보고 매우 놀랐다. 해당 연산은 i와 j를 이진수로 바꾼 다음 or 연산을 사용한다.
이 문제를 통해 rjust(), zfill(), bin(i | j)를 익히고 가길 바란다.
def solution(n, arr1, arr2):
answer = []
arr1 = ['0' * (n - len(bin(i)[2:])) + bin(i)[2:] for i in arr1]
arr2 = ['0' * (n - len(bin(i)[2:])) + bin(i)[2:] for i in arr2]
for i in range(n):
t = ''
for j in range(n):
if arr1[i][j] == '0' and arr2[i][j] == '0':
t += '0'
else:
t += '1'
answer.append(t)
for i in range(n):
answer[i] = answer[i].replace('1', '#')
answer[i] = answer[i].replace('0', ' ')
return answer
🔑 key point: replace()
Other Solution
def solution(n, arr1, arr2):
answer = []
for i, j in zip(arr1, arr2):
t = str(bin(i | j)[2:])
t = t.rjust(n, '0')
t = t.replace('1', '#')
t = t.replace('0', ' ')
answer.append(t)
return answer
🔑 key point: zip(), bin(i | j), rjust()(zfill()과 유사한 기능)
'CodingTest > programmers' 카테고리의 다른 글
[프로그래머스/Programmers] 크레인 인형뽑기 게임 - Python (0) | 2021.09.05 |
---|---|
[프로그래머스/Programmers] [1차] 다트게임 - Python (0) | 2021.09.05 |
[프로그래머스/Programmers] 키패드 누르기 - Python (0) | 2021.09.04 |
[프로그래머스/Programmers] 신규 아이디 추천 - Python (1) | 2021.09.04 |
[프로그래머스/Programmers] 로또의 최고 순위와 최저 순위 - Python (0) | 2021.09.04 |
Comments