brewersfan1976

most_frequent_digit_sum.rb

Oct 4th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.55 KB | None | 0 0
  1. def mostFrequentDigitSum(n)
  2.     amount = Array.new
  3.     x = 0
  4.     y = 0
  5.     number = n
  6.     sum = 0
  7.     string = n.to_s
  8.    
  9.     while (number > 0)
  10.           while (x < string.length)
  11.                 sum = sum + string[x].to_i
  12.                 x = x + 1
  13.           end
  14.          
  15.           amount[y] = sum
  16.           y = y + 1
  17.           number = number - sum
  18.           sum = 0
  19.           x = 0
  20.           string = number.to_s
  21.     end
  22.    
  23.     amount = amount.sort
  24.     amount = amount.reverse
  25.    
  26.     return amount.max_by {|i| amount.count(i)}
  27. end
Advertisement
Add Comment
Please, Sign In to add comment