본문 바로가기
Problem Solving/Codility

[Python] 1.1. BinaryGap

by 부르르 2021. 3. 6.

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

 

1. Iterations lesson - Learn to Code - Codility

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

app.codility.com


자연수를 이진수로 바꾼 후 1의 인덱스를 저장한다.

이후 인덱스간 차이를 계산해서 가장 큰 값을 최대값으로 업데이트해서 답을 구한다.

def solution(N): ​​​​bi = str(bin(N)) ​​​​bi = bi[2:] ​​​​max = 0 ​​​​lst = [] ​​​​for idx in range(len(bi)): ​​​​​​​​if bi[idx] == "1": ​​​​​​​​​​​​lst.append(idx) ​​​​for i in range(len(lst)-1): ​​​​​​​​tmp = lst[i+1] - lst[i] -1 ​​​​​​​​if max < tmp: ​​​​​​​​​​​​max = tmp ​​​​return max

오랜만에 하니까 조금 헤맸었던 문제이다. 기초를 다시 쌓아야지.

728x90
반응형

'Problem Solving > Codility' 카테고리의 다른 글

[Python] 3.3. TapeEquilibrium  (0) 2021.03.11
[Python] 3.2. PermMissingElem  (0) 2021.03.11
[Python] 3.1. FrogJmp  (0) 2021.03.07
[Python] 2.2. OddOccurrencesInArray  (0) 2021.03.07
[Python] 2.1. CyclicRotation  (0) 2021.03.06

댓글