Skip to content

Instantly share code, notes, and snippets.

@mikedemers
Created July 16, 2012 23:57
Show Gist options
  • Save mikedemers/3125982 to your computer and use it in GitHub Desktop.
Save mikedemers/3125982 to your computer and use it in GitHub Desktop.
ghetto histo
#!/usr/bin/env ruby
def histogram(h, width=40)
max_key_size, max_value = 0, 0
h.each_pair do |k,v|
s = k.to_s.size
max_key_size = s if s > max_key_size
max_value = v if v > max_value
end
k_fmt = "%#{max_key_size}s"
v_fmt = "%#{max_value.to_s.size}s"
h.keys.sort.each do |k|
ks = k_fmt % [ k.to_s ]
vs = v_fmt % [ h[k].to_s ]
bar = '=' * (h[k].to_f / max_value * width).floor
puts "#{ks} [#{vs}] #{bar}"
end
end
counts = Hash.new {|h,k| h[k] = 0 }
MAX = 14
STDIN.each_line do |line|
next unless line =~ /^(\d+),(\d+)/
days, count = $1.to_i, $2.to_i
# puts "#{count} at #{days} days"
if days < 7
counts[7] += count
elsif days < 14
counts[14] += count
elsif days < 30
counts[30] += count
elsif days < 60
counts[60] += count
elsif days < 90
counts[90] += count
elsif days < 180
counts[180] += count
elsif days < 365
counts[365] += count
else
counts[366] += count
end
#if days < 14
# counts[days] = count
#else
# wks = (days.to_f / 7.0).floor * 1000
# counts[wks] ||= 0
# counts[wks] += count
#end
end
histogram(counts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment