Python/Codility BinaryGap

[Python/Codility] BinaryGap

📌문제링크

코딜리티는 testCase를 다 맞췄더라도 방심하면 안된다.
쉽게 코드를 제출했으나 결과는 엉망진창이였다. 꼼꼼하게 문제를 읽자


solution

1
2
3
4
5
6
7
8
9
10
11
12
13
def solution(N):
    N = bin(N)[2:]
    arr = []
    for idx, value in enumerate(N):
        if value=='1':
            arr.append(idx)
    arr2= []
    if len(arr) > 1:
        for i in range(len(arr)-1):
            arr2.append(arr[i+1]-arr[i]-1)
        return max(arr2)
    else : 
        return 0

다른사람 풀이

엄청나게 간단한 방법이다.
가장 먼저 양끝 0 제거 -> 양끝 1 제거 -> 1로 split 한 후 max 길이 체크

1
2
def solution(N):
    return len(max(bin(N)[2:].strip('0').strip('1').split('1')))