16 Wed
๋น๋ฐ์ง๋
'''
https://programmers.co.kr/learn/courses/30/lessons/17681
๋น๋ฐ์ง๋
1. array์ ์์๋ ์ด๋ค ์ด์ง์์ ์ญ์ง์. ์ด ์ด์ง์๋ n*n์ ๋ํ ์ํธ
=> ๋ ๊ฐ์ array๋ฅผ ํฉํ array๋ ์ต์ข
์ํธ์ด๋ค. 1์ #์ผ๋ก 0์ ๊ณต๋ฐฑ์ผ๋ก ํํ
=> bin์ผ๋ก ์ด์ง์ ์นํ. zip์ผ๋ก ๋ฐ๋ก ๋ฆฌ์คํธํ. re.sub์ผ๋ก 1๊ณผ 0 ๋์ฒด
'''
import re
def solution(n, arr1, arr2):
cipher = [bin(i | j)[-n:] for i, j in zip(arr1, arr2)]
for i in range(len(cipher)):
cipher[i] = re.sub('1','#',cipher[i])
cipher[i] = re.sub('[0b]',' ',cipher[i])
return cipher
'''
๋ฐฐ์ธ๋งํ ํ์ด.
1) rjust ํจ์ => ์ค๋ฅธ์ชฝ ์ ๋ ฌ ํ ํด๋น ํฌ๊ธฐ ๋งํผ ๋ฌธ์์ด ์์ฑ, ๋น ๊ณต๊ฐ์ ํด๋น ๋ฌธ์๋ก ์นํ
2) re.sub ๋์ ๊ฐ๋จํ๊ฒ replace ์ฌ์ฉ ๊ฐ๋ฅ
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
'''
'''
ํ์ค ํ์ด
1) ๋ฌธ์์ด ํฌ๋งทํฐ๋ฅผ ์ด์ฉ => int: {0:d}, bin: {0:b}, oct: {0:o}, hex: {0:x}".format(num)
2) ์ rjust์๋ 0์ผ๋ก๋ง ์ฑ์ธ ์ ์๋ ์ ์ ์ ์ธํ๊ณ ๋ ๋น์ท.
=> str.zfill(n) : ํด๋น ์๋ฆฌ์๊ฐ ๋๋๋ก ์์ 0์ ์ฑ์
return [''.join(map(lambda x: '#' if x=='1' else ' ', "{0:b}".format(row).zfill(n))) for row in (a|b for a, b in zip(arr1, arr2))]
'''
Last updated
Was this helpful?