DEV Community

loading...

Discussion on: Daily Challenge #238 - One is the Loneliest Number

Collapse
citizen428 profile image
Michael Kohl

Ruby:

def lonely_num(n)
  digits = n.to_s.chars.map(&:to_i)
  digits.min_by.with_index do |n, i|
    range = digits[[0, i - n].max..i + n]
    [range.sum - n, range.length - 1]
  end
end

lonely_num(s)
#=> 1
lonely_num("23456")
#=> 2
lonely_num("42435")
#=> 2