From bd723335cb82e8609d0e591945337ab5942b3d23 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Thu, 11 Jan 2024 10:30:53 +0000 Subject: avg: running average --- avg | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'avg') diff --git a/avg b/avg index 5d32bb8..ac912d6 100755 --- a/avg +++ b/avg @@ -4,7 +4,17 @@ require 'optimist' conf = Optimist::options do banner "avg < " - opt :round, "Number of digits after decimal point.", :type => :int, :default => -1 + opt :round, "Number of digits after decimal point.", :type => :int, :default => -1, :short => "-r" + opt :Running, "Running average", :type => :bool, :default => false, :short => "-R" +end + +def print_avg sum, i, round + avg = sum / i.to_f + if round >= 0 + puts avg.round round + else + puts avg + end end sum = 0.0 @@ -12,13 +22,11 @@ i = 0 while line = STDIN.gets sum += line.to_f i +=1 + if conf[:Running] + print_avg sum, i.to_f, conf[:round] + end end -avg = sum/i.to_f - -if conf[:round] >= 0 - puts avg.round conf[:round] -else - puts avg +if not conf[:Running] + print_avg sum, i.to_f, conf[:round] end - -- cgit v1.2.3