본문 바로가기
개발/알고리즘

[codility] BinaryGap

by ny0011 2021. 2. 16.
반응형

app.codility.com/programmers/lessons/1-iterations/binary_gap/

 

BinaryGap coding task - Learn to Code - Codility

Find longest sequence of zeros in binary representation of an integer.

app.codility.com

 

def solution(N):
    # write your code in Python 3.6
    start = 0
    end = 0
    max_count = 0
    n_str = str(bin(N))[2:]
    for idx, v in enumerate(n_str):
        if v == "1":
            max_count = max(max_count, end-start)
            start=idx
            end=idx
        else:
            end +=1
    if n_str[-1] == "1":
        max_count = max(max_count, end-start)
    return max_count

'개발 > 알고리즘' 카테고리의 다른 글

[codility] OddOccurrencesInArray  (0) 2021.02.16
[codility] CyclicRotation  (0) 2021.02.16
[프로그래머스] 이진 변환 반복하기  (0) 2021.02.15
[프로그래머스] 행렬의 곱셈  (0) 2021.02.15
[프로그래머스] 카펫  (0) 2021.02.15

댓글