Skip to content

Instantly share code, notes, and snippets.

@thalesmello
Created July 26, 2014 00:51
Show Gist options
  • Save thalesmello/d71f03553c90e83051e8 to your computer and use it in GitHub Desktop.
Save thalesmello/d71f03553c90e83051e8 to your computer and use it in GitHub Desktop.
histogram puts
def histogram(data, bins = 10, length = 100)
min, max = data.minmax
mod = max - min
histog = data.group_by { |v| ((v - min).to_f / mod * bins).to_i }.map { |bin, values| [bin, values.count] }.to_h
max_value = histog.map { |_, value| value }.max
histog_array = [0] * bins
histog = histog.each { |bin, value| histog_array[bin] = (value.to_f / max_value * length).to_i }
puts histog_array.to_s
puts min.to_s + " " * length + max_value.to_s
histog_array.each { |value| puts "*" * value.to_i }
puts max
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment