Python Forum
Codility Binary Gap Exercise
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Codility Binary Gap Exercise
#2
The solution for trailing zeros is actually contained in the description of binary gap; contiguous zeros surrounded by ones. So a gap will be preceded and followed by a 1.
set gap count and max gap to zero
set saw_one to False.
while number is greater than zero
    if number is odd this is the end of a gap.
        if saw_one
            Check if this was largest gap
        set gap count to zero
        set saw_one to True
    else 
        increment gap count
    shift number right
You have all the parts, but your logic on what to do when you see a 1 is a bit off. I see no reason to build a list of zeros and ones and then count using the list. Get rid of the list and count the zeros in the code that is getting the bits.
Reply


Messages In This Thread
Codility Binary Gap Exercise - by Westerner - Jan-08-2021, 09:51 AM
RE: Codility Binary Gap Exercise - by deanhystad - Jan-08-2021, 03:59 PM
RE: Codility Binary Gap Exercise - by Westerner - Jan-08-2021, 09:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Codility Frog River One Westerner 2 2,438 Jan-09-2021, 06:35 PM
Last Post: deanhystad
  hex file to binary or pcap to binary baran01 1 5,737 Dec-11-2019, 10:19 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020