Codility - Binary Gap - Solution

Publicado el: 19 octubre 2021
en el canal de: Coding Hacks
297
1

Codility - Binary Gap - Solution
=========================================================
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
pass

#Given Any Number N , we have to find Binary Equivalent of N
#bin(N)
#From there we have to find the Number of 0s Together
#We can find it by converting the data to text and splitting it by the value of 1
#if you are not getting it completely its ok.
#when we write it u will get it for sure
#after that we have to find which element in the split array has the max length
#we will be found with our answer
#lets start

#always put everything under try catch so that u dont end up in big problems later
try:
b = bin(N) # Converting the Given Number to Binary
c = b.replace('0b','') # we just want the number and not the prefix of the type of number
so lets replace it with nothing

d = c.split('1') # now split the whole number by the value of 1
by means of this operation we got all the 0s which are together
but there is a condition that if the 0s are not followed by 1s then it is not a binary gap
so in this case we can simply remove the last element whether it be binary gap or not
because it makes no sense to find it as there will be no 1s which will follow
# so pop only the last element
e = d.pop()
print(e)

n = 0 #initialise a temporary count

traverse through all the elements of the final array
if length of any of the element is greater than the existing binary gap
then it is the longest binary gap
Instead of Popping We are just traversing only till N-1 th Element
But Ideally pop should equally work but dont wanna debug too much

for i in range(0,len(d)-1):
if(len(d[I] gt n):
n=len(d[i])
print(b)
print(c)
print(d)
print(n)
return n
except:
return 0
we are almost done
Lets try and submit the test if the performance is ok


En esta página del sitio puede ver el video en línea Codility - Binary Gap - Solution de Duración hora minuto segunda en buena calidad , que subió el usuario Coding Hacks 19 octubre 2021, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 297 veces y le gustó 1 a los espectadores. Disfruta viendo!