ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

https://app.codility.com/programmers/lessons/1-iterations/

๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ, ์ด์ง„์ˆ˜์—์„œ 1๊ณผ ๋‹ค์Œ์— ๋‚˜ํƒ€๋‚˜๋Š” 1์‚ฌ์ด์˜ ์ฐจ๊ฐ€ ๊ฐ€์žฅ ํฐ ๊ฒฝ์šฐ๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ์ด๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ์ฃผ์–ด์ง„ ์ˆ˜์˜ ์ด์ง„์ˆ˜๊ฐ€ 1001์ด๋ฉด ์ฐจ๋Š” 2๊ฐ€ ๋˜๊ณ , 1000์ผ ๊ฒฝ์šฐ 0์ด ๋œ๋‹ค.

๋ฌธ์ œ ํ’€์ด

1์ด ๋“ฑ์žฅํ•œ ์ดํ›„๋กœ 1์ด ๋‹ค์‹œ ๋ฐ˜๋ณต๋  ๋•Œ๊นŒ์ง€ ์นด์šดํŠธํ•˜์—ฌ, ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค.

์ฝ”๋“œ

def solution(N):
    answer = 0
    cnt = 0
    binary = format(N, 'b')
    for num in binary:
        num = int(num)
        if not num:
            cnt += 1
        else:
            if cnt > answer:
                answer = cnt
            cnt = 0
    return answer
  • format์„ ํ†ตํ•ด ์ด์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•  ๊ฒฝ์šฐ, ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ’์˜ ํƒ€์ž…์€ String์ด๋‹ค.
728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€