Skip to content

Instantly share code, notes, and snippets.

@rayning0
Created March 29, 2017 20:14
Show Gist options
  • Select an option

  • Save rayning0/7f5b2d2bf0e773c31b3a1d6b1250fb8d to your computer and use it in GitHub Desktop.

Select an option

Save rayning0/7f5b2d2bf0e773c31b3a1d6b1250fb8d to your computer and use it in GitHub Desktop.
# Both these solutions got 100% on Codility
def binary(m)
bin = ''
begin
bin.insert(0, (m % 2).to_s)
m /= 2
end until m == 0
bin
end
def solution(n)
bin = binary(n) # or n.to_s(2) does direct binary conversion
zeroes = bin.split('1')
return 0 if zeroes.empty?
zeroes.pop unless bin[-1] == '1'
zeroes.max.length
end
# https://codility.com/demo/results/trainingMUNSAW-9C8/
def solution2(n)
bin = n.to_s(2) # or n.to_s(2) does direct binary conversion
zeroes = bin.split(/1/)
return 0 if zeroes.empty?
zeroes.pop unless bin[-1] == '1'
zeroes.max.length
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment