๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš€ Development/Problem Solving

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค, 2018 KAKAO BLIND RECRUITMENT] [1์ฐจ] ๋น„๋ฐ€์ง€๋„(Lv1) - ํŒŒ์ด์ฌ(Python)

by Jay Din 2024. 9. 5.
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/17681

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

๋ฌธ์ œํ’€์ด

1. ๋ฌธ์ œ ๊ฐœ์š”

  • ์ง€๋„1 ๊ณผ ์ง€๋„2 ๋Š” ๊ฐ๊ฐ ์ •์ˆ˜ ๋ฐฐ์—ด๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
  • ์ด ๋ฐฐ์—ด์„ ์ด์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ํ•ด๋…ํ•˜๊ณ , ์ด๋ฅผ ํ†ตํ•ด ์ตœ์ข… ์ง€๋„๋ฅผ ์™„์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • ์ง€๋„์˜ ๊ฐ ์นธ์€ ๋ฒฝ('#') ๋˜๋Š” ๊ณต๋ฐฑ(' ') ์œผ๋กœ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค.
  • ๋‘ ์ง€๋„ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ๋ฒฝ(1)์ธ ๊ฒฝ์šฐ ํ•ด๋‹น ์นธ์€ ๋ฒฝ(#)์ด ๋˜๊ณ , ๋‘˜ ๋‹ค ๊ณต๋ฐฑ(0)์ด๋ฉด ๊ณต๋ฐฑ( )์ด ๋ฉ๋‹ˆ๋‹ค.

 

2. ์ฝ”๋“œํ•ด์„ค

def makeNumber(arr, n):
    makeArr = []
    for i in arr:
        temp = i
        tempArr = [0]*n
        for j in range(n):
            if temp % 2 == 1:
                tempArr[j] = 1
            temp //= 2 
        tempArr.reverse()
        makeArr.append(tempArr)
    return makeArr
  • makeNumber ํ•จ์ˆ˜๋Š” ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ 2์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ n๊ฐœ์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง„ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.
    • temp๋Š” arr ๋ฐฐ์—ด์˜ ๊ฐ ์ˆซ์ž๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
    • ์ˆซ์ž๋ฅผ 2๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€(์ฆ‰, temp % 2)๋กœ ๊ทธ ์ˆซ์ž์˜ ๋งˆ์ง€๋ง‰ ์ž๋ฆฌ๊ฐ€ 1์ธ์ง€ 0์ธ์ง€ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค.
    • ๋งˆ์ง€๋ง‰ ์ž๋ฆฌ๊ฐ€ 1์ด๋ฉด temparr๋ฐฐ์—ด์˜ ํ•ด๋‹น ์ž๋ฆฌ์— 1์„ ๋„ฃ๊ณ , 0์ด๋ฉด 0์„ ๋„ฃ์Šต๋‹ˆ๋‹ค.
    • 2๋กœ ๋‚˜๋ˆˆ ๊ฐ’์„ ๋ชจ์€ ๋ฆฌ์ŠคํŠธ tempArr์„ ์—ญ์ (reverse)์„ ์‹œ์ผœ์•ผ 2์ง„์ˆ˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.
def solution(n, arr1, arr2):
    answer = []
    
    arr11 = makeNumber(arr1, n)
    arr22 = makeNumber(arr2, n)
    
    for i in range(n):
        res = ''
        for j in range(n):
            if arr11[i][j] == 1 or arr22[i][j] == 1:
                res += '#'
            else:
                res += ' '
        answer.append(res)
    
    return answer
  • arr11 = makeNumber(arr1, n)
    • arr1 ๋ฐฐ์—ด์„ 2์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•œ ๊ฒฐ๊ณผ๋ฅผ arr11์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
    • arr2๋„ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ 2์ง„์ˆ˜ ๋ณ€ํ™˜ ๊ฐ’์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
  • ์ตœ์ข… ์ง€๋„ ํ•ด๋…
    • ๋‘ ๋ณ€ํ™˜๋œ ์ง€๋„๋ฅผ ๋น„๊ตํ•˜๋ฉด์„œ, ๊ฐ ์œ„์น˜์—์„œ ํ•˜๋‚˜๋ผ๋„ 1์ด ์žˆ์œผ๋ฉด ํ•ด๋‹น ์œ„์น˜๋Š” ๋ฒฝ(#), ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๊ณต๋ฐฑ( )์œผ๋กœ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.
    • ์ด๋ฅผ res ๋ฌธ์ž์—ด์— ์ €์žฅํ•˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ answer ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

 

์ตœ์ข… ์ฝ”๋“œ

def makeNumber(arr, n):
    makeArr = []
    for i in arr:
        temp = i
        tempArr = [0]*n
        for j in range(n):
            if temp%2 == 1:
                tempArr[j]=1
            temp //= 2 

        tempArr.reverse()
        makeArr.append(tempArr)

    return makeArr

def solution(n, arr1, arr2):
    answer = []
    

    
    arr11 = makeNumber(arr1, n)
    arr22 = makeNumber(arr2, n)
    
    for i in range(n):
        res = ''
        for j in range(n):
            if arr11[i][j] == 1 or arr22[i][j] == 1:
                res+='#'
            else:
                res+=' '
                
        answer.append(res)
        
    
    return answer

 

728x90
๋ฐ˜์‘ํ˜•