DEV Community

loading...

Discussion on: Daily Challenge #260 - Subtract the Sum

Collapse
lxxxvi profile image
Mario

Yanother Ruby solution

FRUIT = {
  '1' => 'kiwi',
# ...
  '100' => 'pineapple'
}

def subtract_sum(number)
  return unless (10...10_000).cover?(number)

  loop do
    number_split = number.to_s.split('')
    to_subtract = number_split.reduce(0) { |sum, i| sum += i.to_i }
    number -= to_subtract

    return FRUIT[number.to_s] if FRUIT.key?(number.to_s)
  end
end