I want to simulate in ruby my implementation of the map
and reduce
functions for a system like hadoop to verify that the idea works at least.
I have the following problem. I have two lists of elements:
List1
3 - A
4 - B
5 - C
7 - D
8 - F
List2
2 - A
8 - B
6 - C
9 - D
4 - E
I need to build a common list that includes the sum of the numbers associated with the alphabets common in the two lists:
commonList
5 - A
12 - B
11 - C
16 - D
I want to make a ruby script with the map
and reduce
operations to solve this problem. I am unsure how to tackle on this problem or what procedure to follow to simulate this in a ruby script.
Any help appreciated.
Using irb (ruby-1.9.2-p180):
list = [ {a:2, b:1, d:3}, {a:3, b:2, c:3}, {a:4, b:1, c:3} ]
=> [{:a=>2, :b=>1, :d=>3}, {:a=>3, :b=>2, :c=>3}, {:a=>4, :b=>1, :c=>3}]
Hash[list.map(&:keys).inject(&:&).map{|key| [key,list.map{|arr| arr[key]}.inject(&:+)]}]
=> {:a=>9, :b=>4}
this solution works with multiple arrays (2+) it finds common keys and sums them returning a hash of results
to find common keys (gather keys and find common part):
list.map(&:keys).inject(&:&)
to find sum for key (select values by keys and sum them):
list.map{|arr| arr[key]}.inject(&:+)
to build Hash from array of pairs [[:a,9], [:b,4]]
:
results = [[:a,9], [:b,4]]
Hash[ results ]
I love ruby for this one liners!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With