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?