Skip to content

Instantly share code, notes, and snippets.

View dpellegrini's full-sized avatar

David Pellegrini dpellegrini

  • Spoke
  • Silicon Valley
View GitHub Profile
@dpellegrini
dpellegrini / binary_gap.rb
Created February 3, 2022 01:15
Naïve binary gap implementation in Ruby
def binary_gap(n)
n.is_a?(Integer) or raise(ArgumentError, 'Must be an Integer')
bits = n.digits(2)
longest = current = 0
bits.each do |d|
if d == 0
current += 1
else
longest = current if (current > longest)
current = 0