Binary Gap in Python and C++ Codility Solutions Lesson 1

preview_player
Показать описание
This video describes the Binary Gap Algorithm problem of Codility and presents two solutions one in C++ and the other in Python language. Description of the problem itself is kept brief because it it found on codility website and more emphasis is put on the solution details. This is meant for beginners to help develop algorithmic skills and test examples in Python and C++. Exercise python algorithms interview.

🍓 If you want to follow structured courses with more details and practice exercises check my "About" page for Discount Coupons on my Udemy courses covering: Python basics, Object Oriented Programming and Data Analysis with NumPy and Pandas, ... more courses are on the way drop me a message if you have a particular interesting topic! Good luck!

00:00 Problem Intro
01:06 Solution description
03:46 C++ solution
07:30 Python solution
Рекомендации по теме
Комментарии
Автор

For anyone looking for a solution without external function, here is my solution in python. Upvote for visibility to help others!

def solution(N:int):
max_gap= 0
count=0
flag=False
while N>=1:
if N%2==0:
if flag==True:
count=count+1
else:
flag=True
if max_gap<count:
max_gap=count
count=0
N=N//2

return max_gap

divyathomas
Автор

hey man, your Codility Solutions playlist is a Godsent. I have a codility interview with BMW in a week's time and I'm going to use your playlist for the prep. I've never done a codility interview before, I'm so bad the first time I did a demo test on the website today, I got 0% on everything; I couldn't even come up with a correct brute force solution. Started looking for help on YT and found your playlist.
As my approach, I will watch the whole playlist first paying attention to your problem solving approach and how you develop the actual algorithms. I figure this will help in identifying patterns and recurring methods in the solutions. Then I will start solving the problems myself on the website top down.
I'll give updates on my progress and any issues in the comments of the videos. And of course I'll let you know how the coding interview itself goes. If you or anyone else have tips for me, just lemme know in the comments.

wonderjaxe
Автор

Very nice! Thank you.

I want to say that you can also use f string representation instead of bin():
N = f'{N:08b}'
e.g.
29 > 00011101 >> you would get 3
then you can do:
N = f'{N:b}'
29 >> 11101 >> binary gap 1

deutscher
Автор

def solution(N):
lenZeros = (list(map(lambda x:len(x), (bin(N)[2:]).split('1'))))[1:-1]
return 0 if lenZeros==[] else max(lenZeros)

kipkiruihillary
Автор

def solutions(number: str) -> int:
gap = 0
gap_temp = 0

for n in number:
if int(n) == 1:
gap = max(gap_temp, gap)
gap_temp = 0
continue # dont continue to next line

# come here if n equal to 0
gap_temp += 1

return gap

Selina_oficial
Автор

How about pushing the values to a stack? Then they aren't backwards -- you just pop the stack until empty.

KatoKrazy
Автор

thanks, I tried the python code with N=100 , it gives max value two instead of one, how to prevent from countingg the last distance if its not between two ones

marwanghubein
Автор

is there a way to find the binary gap using a recursive function?

ULTIMARAGNARK
Автор

If i try to include vector.h in codility IDE, I get error ... how come?

jonnyreh
Автор

In the python solution I am getting error in the elif line any advice how to fix it ?

zeynabyassin
Автор

Wrong Answer In 1041 Can't understand Why

HypeCentral-ro
Автор

I've got an error :(
Error: Too many test cases (10 maximum)

Adydobre
Автор

This solution doesn't work with leetcode 868 Binary Gap

Given a positive integer n, find and return the longest distance between any two adjacent 1's in the binary representation of n. If there are no two adjacent 1's, return 0.

Two 1's are adjacent if there are only 0's separating them (possibly no 0's). The distance between two 1's is the absolute difference between their bit positions. For example, the two 1's in "1001" have a distance of 3.

This works.. you have to set b = 1 since they seemed to have changed the question and redefined adjacent
class Solution:
def binaryGap(self, n: int) -> int:
n = bin(n)[2:]
print(n)
b = 0
maxb = 0

for k in n:
if int(k) == 0:
b +=1
elif int(k) == 1:
maxb = max(b, maxb)
b = 1
return maxb

ripperx
join shbcf.ru