summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Simianer <patrick@lilt.com>2024-01-11 10:30:53 +0000
committerPatrick Simianer <patrick@lilt.com>2024-01-11 10:30:53 +0000
commitbd723335cb82e8609d0e591945337ab5942b3d23 (patch)
treedd122b88ff6e0ab4f6ca730fca841917acb92d2e
parent5cc3991225b7fa474100ace8bc11f3a04068debb (diff)
avg: running average
-rwxr-xr-xavg24
1 files changed, 16 insertions, 8 deletions
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 < <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
-