Language/Python

[Python]Practice algorithm - Binary Gap

청렴결백한 만능 재주꾼 2020. 7. 27. 16:57
반응형

A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.

 

Write a function:

def solution(N):
	

that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.

 

 

 

My answer:

def solution(N):
  a= max(bin(N).split('b')[1].strip('0').strip('1').split('1'))
  return len(a)
반응형