Sha256: 4732ff5bb67ca5c7f202d51a16fde7dec06d0869a8487864ad940c1cd37cc777
Contents?: true
Size: 1.93 KB
Versions: 15
Compression:
Stored size: 1.93 KB
Contents
#!/usr/bin/env ruby # encoding: utf-8 # Percentage bar. # # items = (1..100).to_a # size = (10..100).to_a # length = size.shuffle.first # # items.each do |item| # puts Percentage.new(100, item, length).to_bar # end # class Percentage def initialize(product, divisor, scale = 100) @product = product @scale = scale - 5 @pre_percentage = (divisor * @scale / product).to_f @percentage = (@pre_percentage * 100 / @scale) @divisor = divisor end def to_i @percentage.to_i end def to_f @percentage.to_f end # 0 ~ 50 green # 50 ~ 80 yellow # 80 ~ 100 red def to_bar(color = true, bar = '|') filling = (bar * @pre_percentage.to_i) padding = 0 if color if to_i >= 0 and to_i < 50 filling = "[0;32;1m" + (bar * @pre_percentage.to_f) + "[0m" end if to_i >= 50 and to_i < 80 filling = "[0;33;1m" + (bar * @pre_percentage.to_f) + "[0m" end if to_i >= 80 filling = "[0;31;1m" + (bar * @pre_percentage.to_f) + "[0m" end if filling.size > 0 padding = 13 end end bindings = [filling, to_f, '%', @divisor] "[%-#{@scale + padding}s%7.3f%s] %10.2f incidents for" % bindings end end class Array def mean product = 0; each do |number| product += number end product / size end def product sum = 0; each do |number| sum += number end sum end def to_percentage Percentage.new(product, hash.keys.size) end end hash = {} STDIN.each_line do |line| chunks = line.split(/\s+/) hash[chunks[0]] ||= [] hash[chunks[0]] << chunks[1].to_f end total = 0 hash.keys.each do |key| array = hash[key] total += array.product end hash.keys.each do |key| array = hash[key] p = Percentage.new(total, array.product, 50) template = "%s %s" bindings = [p.to_bar, key] puts template % bindings end puts "Total: #{total}"
Version data entries
15 entries across 15 versions & 1 rubygems