기타/Codility

[Easy] BinaryGap

백곰곰 2022. 5. 22. 15:35
728x90
반응형

[문제]

2진수에 대해 1로 둘러싸인 연속된 0의 숫자를 세어 가장 큰 연속된 0의 개수를 출력하는 문제

 

[코드]

# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(N):
    # write your code in Python 3.6
    binary = bin(N)
    start=0
    cont=0
    answer=0
    for b in binary:
        if b=='1' and start==0:
            start=1
        elif b=='0' and start==1:
            cont+=1
        elif b=='1' and start==1:
            answer = max(answer, cont)
            cont=0
    return answer

#MinDistinct is recommanded

* bin 함수를 통해 2진수로 변환 시 string으로 리턴됨

 

 

https://app.codility.com/programmers/trainings/9/binary_gap/

 

BinaryGap coding task - Practice Coding - Codility

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

app.codility.com

 

728x90

'기타 > Codility' 카테고리의 다른 글

[Easy] Distinct  (0) 2022.05.25
[Easy] PassingCars  (0) 2022.05.24
[Easy] FrogJmp  (0) 2022.05.22
[Easy] CyclicRotation  (0) 2022.05.22
[Easy] ArrListLen  (0) 2022.05.22