[codility] Solution to Binary-Gap by codility (python)

728x90
반응형

Question: https://codility.com/demo/take-sample-test/binary_gap

 

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

def solution(N):
    
    cur,max_gap=0,0
    for i,e in enumerate(str(bin(N)[2:])):
        if e=='1':
            max_gap=max(max_gap,cur)
            cur=0
        else:
            cur+=1
    return max_gap

 

728x90
반응형
TAGS.

Comments