Let's say that you have a hash where the keys are Strings, and the values are Floats. You want to group the values by a substring of each key, and then sum the values within each group.
Basically, you want to go from this:
{ "aaaapattern1aaaa" => 213.2342, "pattern2aaaa" => 0.03,
"aaaaapattern3" => 12.1, "pattern1aaa" => 54.4544,
"aaaaapattern2" => 65.003 }
to this:
{"pattern1"=>267.6886, "pattern2"=>65.033, "pattern3"=>12.1}
Here is my current approach:
data = {
"aaaapattern1aaaa"=>213.2342, "pattern2aaaa"=>0.03,
"aaaaapattern3"=>12.1, "pattern1aaa"=>54.4544,
"aaaaapattern2"=>65.003
}
key_regexp = /pattern\d/
intermediate_results = data.map do |key, value|
{ key.match(key_regexp)[0] => value }
end
final_result = intermediate_results.reduce do |cumulative_hash, individual_hash|
cumulative_hash.merge(individual_hash) do |key, old_value, new_value|
old_value + new_value
end
end
How would you improve on this? What factors should be considered in formulating an ideal approach? Would your answer change based on the size of the Hash, and if so, how?