diff options
author | Patrick Simianer <patrick@lilt.com> | 2024-01-11 10:30:53 +0000 |
---|---|---|
committer | Patrick Simianer <patrick@lilt.com> | 2024-01-11 10:30:53 +0000 |
commit | bd723335cb82e8609d0e591945337ab5942b3d23 (patch) | |
tree | dd122b88ff6e0ab4f6ca730fca841917acb92d2e | |
parent | 5cc3991225b7fa474100ace8bc11f3a04068debb (diff) |
avg: running average
-rwxr-xr-x | avg | 24 |
1 files changed, 16 insertions, 8 deletions
@@ -4,7 +4,17 @@ require 'optimist' conf = Optimist::options do banner "avg < <one number per line>" - 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 - |