Skip to content

Instantly share code, notes, and snippets.

Last active Mar 27, 2018
What would you like to do?
Ruby binary gap of an Integer
def bin_gap(number)
bin = number.to_s(2) # convert number to binary
zeros = bin.split('1') # extract 0's in binary
zeros.reject(&:empty?) # remove if any empty elements
zeros.pop if number % 2 == 0 # pop out last 0's
return 0 if zeros.empty?{ |zero| zero.length }.max # max length from 0's array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment