Notice
Recent Posts
Recent Comments
Link
유자차의 재테크 공부방
[프로그래머스] 비밀지도 본문
반응형
# 십진수 to 이진수
bin(9)
#출력
0b1001
bin(9) # 0b1001
bin(30) #0b11110
bin(9|30) # 0b11111
bin(9&30) # 0b1000
# 정해진 자릿수에 빈자리 채우기
# 문자열.rjust([자리수], [채울것]) : 오른쪽부터 채우기
"1001".rjust(5, 0) #01001
# 문자열.ljust([자리수], [채울것]) : 왼쪽부터 채우기
"1001".ljust(5, 0) #10010
풀이방법
def solution(n, arr1, arr2):
ans=[]
for i in range(n):
# 십진수->이진수 -> 합치기
res = str(bin(arr1[i]|arr2[i])[2:])
res = res.replace("1", "#")
res = res.replace("0", " ")
ans.append(res.rjust(n," "))
return ans
def d2b(n, num): # 십진수 -> 이진수
ans=[0]*n
i=1
for _ in range(n-1):
if num==1:
break
ans[-i] = num%2
num=num//2
i+=1
ans[-i] = 1
return ans
def solution(n, arr1, arr2):
ans=[]
# 십진수 -> 이진수
for i in range(n):
res1 = d2b(n, arr1[i])
res2 = d2b(n, arr2[i])
#합치기
res=""
for j in range(n):
if res1[j]==1 or res2[j]==1:
res+="#"
else:
res+=" "
ans.append(res)
return ans
반응형
'파이썬 > 알고리즘 문제 풀이' 카테고리의 다른 글
[프로그래머스] 다리를 지나는 트럭 (0) | 2022.06.28 |
---|---|
[프로그래머스] 주식가격 (0) | 2022.06.27 |
[프로그래머스] 최대공약수와 최소공배수 (0) | 2022.03.19 |
[프로그래머스] 문자열 내 마음대로 정렬하기 (0) | 2022.03.18 |
[프로그래머스] 소수찾기 (0) | 2022.03.05 |
Comments