Python Forum
Codility Binary Gap Exercise
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Codility Binary Gap Exercise
#3
I had the binary compiled into a list so it would be ordered properly.
e.g. integer 32 is 0100000 in the list whereas it's 0000001 otherwise
The output, however, was correct even if it wasn't ordered.
Here's the final, working code:
def solution(N):
    num = N
    streakstart = False
    streak = 0
    longstreak = 0
    while int(num) > 0 :
        bin = num % 2
        num = int(num / 2)
        if bin == 1 :
            streakstart = True
            streak = 0
        elif bin == 0 and streakstart is True :
            streak = streak + 1
        else :
            streak = 0
            continue
        if streak > longstreak :
            longstreak = streak
    return longstreak
    pass
Turns out I was just thinking too hard about it, thinking it needed proper binary order! Doh

Appreciate your help! <:
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,413 Jan-09-2021, 06:35 PM
Last Post: deanhystad
  hex file to binary or pcap to binary baran01 1 5,706 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