
def max_by(arr)
  max_index, result = 0, []
  arr.each.with_index do |elem,idx| 
    result << yield(elem)
    max_index = idx if result[max_index] < result[idx]
  end
  arr[max_index]
end





p max_by([1, 5, 3]) { |value| value + 2 } == 5
p max_by([1, 5, 3]) { |value| 9 - value } == 1
p max_by([1, 5, 3]) { |value| (96 - value).chr } == 1
p max_by([[1, 2], [3, 4, 5], [6]]) { |value| value.size } == [3, 4, 5]
p max_by([-7]) { |value| value * 3 } == -7
p max_by([]) { |value| value + 5 } == nil