Ruby Array to Histogram: how to group numbers by range?

I am trying to group an array of integers into a hash based on where the individual values ​​fall into the range. Basically, I want to convert an array to a fixed-width histogram.

Example:

values = [1,3,4,4,4,4,4,10,12,15,18] bin_width = 3 

I need to group the values ​​of an array into a historical schema based on a range where they fall into a bucket 3 units wide, for example:

 {'0..2'=>[1,3],'3..5'=>[4,4,4,4,4],'6..8'=>[],'9..11'=>[10].... 

Is there one single-line solution (maybe something like values.group_by{|x| #range calc} ) that will work here?

+7
source share
2 answers
 values = [1, 7, 2, 8, 2] values.group_by { |x| x / 3 }.map { |k, vs| [(3*k..3*k+2), vs] }.to_h #=> {0..2=>[1, 2, 2], 6..8=>[7, 8]} 

If you really need empty ranges, I don't think a clean single line is possible. But this should do:

 grouped = values.group_by { |x| x / 3 } min, max = grouped.keys.minmax (min..max).map { |n| [(3*n..3*n+2), grouped.fetch(n, [])] }.to_h #=> {0..2=>[1, 2, 2], 3..5=>[], 6..8=>[7, 8]} 
+10
source

I came up with a rather ineffective, but quite clear solution:

 ranges = 0.step(values.max, bin_width).each_cons(2).map { |s, e| Range.new(s, e, true) } values.group_by { |v| ranges.find { |r| r.cover? v } } 
+4
source

All Articles