lib/genevalidator/ext/array.rb in genevalidator-1.6.12 vs lib/genevalidator/ext/array.rb in genevalidator-2.1.3

- old
+ new

@@ -7,28 +7,45 @@ def mean sum / length.to_f end - def median - sorted = sort + def median(already_sorted = false) + sorted = already_sorted ? self : sort len = sorted.length (sorted[(len - 1) / 2] + sorted[len / 2]) / 2.0 end def mode - freq = inject(Hash.new(0)) { |h, v| h[v] += 1; h } - sort_by { |v| freq[v] }.last + freq = each_with_object(Hash.new(0)) { |v, h| h[v] += 1; } + max_by { |v| freq[v] } end def sample_variance m = mean sum = inject(0) { |accum, i| accum + (i - m)**2 } sum / (length - 1).to_f end def standard_deviation Math.sqrt(sample_variance) + end + + def all_quartiles + sorted = sort + len = sorted.length + split = sorted.median_split + [ + split[0].median(true), + sorted.median(true), + split[1].median(true) + ] + end + + def median_split + len = length + center = len % 2 + [self[0..len / 2 - 1], self[len / 2 + center..-1]] end end end class Array