loading...

re: Daily Challenge #238 - One is the Loneliest Number VIEW POST

FULL DISCUSSION
 

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
code of conduct - report abuse