Created
June 5, 2020 20:54
-
-
Save cardotrejos/e8a55d02d2338b733a5e4e6d697b8bac to your computer and use it in GitHub Desktop.
Function def binary_gap(n) that, given a positive integer N, returns the length of its longest binary gap.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def bin_gap (n) | |
bin = n.to_s(2) | |
temp = [] | |
gaps = [] | |
bin.each_char do |char| | |
if char == "1" | |
unless temp.empty? | |
gaps << temp.length | |
temp = [] | |
end | |
else | |
temp << char | |
end | |
end | |
return 0 if gaps.empty? | |
gaps.max | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment