Logo

Programming-Idioms

History of Idiom 195 > diff from v5 to v6

Edit summary for version 6 by steenslag:
New Ruby implementation by user [steenslag]

Version 5

2019-09-29, 11:22:48

Version 6

2019-10-05, 22:04:51

Idiom #195 Pass a two-dimensional array

Pass an array a of real numbers to the procedure (resp. function) foo. Output the size of the array, and the sum of all its elements when each element is multiplied with the array indices i and j (assuming they start from one).

Idiom #195 Pass a two-dimensional array

Pass an array a of real numbers to the procedure (resp. function) foo. Output the size of the array, and the sum of all its elements when each element is multiplied with the array indices i and j (assuming they start from one).

Code
def foo(ar)
  puts "Array size: #{ar.size}, #{ar.max.size}."
  ar.each.with_index(1).sum do |a, i|
    a.each.with_index(1).sum do |el, j|
      el*i*j
    end
  end
end

puts foo(array)