ruby - Comparing numbers in an array, outputting highest number -


i need write function takes 3 phone numbers in array, adds digits of each number seperately, , outputs phone number biggest value on screen.

numbers in form [821-839-1182, 128-389-........]

you this:

arr = ['821-839-1182', '128-389-4732', '621-411-7324']  arr.max_by { |s| s.each_char.map(&:to_i).reduce(:+) }   #=> "128-389-4732" 

we have:

a = arr.map { |s| s.each_char.map(&:to_i) }   #=> [[8, 2, 1, 0, 8, 3, 9, 0, 1, 1, 8, 2],   #    [1, 2, 8, 0, 3, 8, 9, 0, 4, 7, 3, 2],   #    [6, 2, 1, 0, 4, 1, 1, 0, 7, 3, 2, 4]]   b = a.map { |e| e.reduce(:+) }   #=> [43, 47, 31] 

as largest sum @ index 1, max_by return string @ index of arr. note '-'.to_i #=> 0.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -