blob: cc4c0e604b47ca230bfda56342aa9b4d9f018fa2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
#!/usr/bin/env ruby
require 'trollop'
def usage
STDERR.write "./avg [-r <d>] < <one number per line>\n"
exit 1
end
usage if not [0,2].include? ARGV.size
opts = Trollop::options do
opt :round, "Number of digits after decimal point.", :type => :int, :default => -1
end
sum = 0.0
i = 0
while line=STDIN.gets
sum += line.strip.to_f
i +=1
end
avg = sum/i.to_f
if opts[:round] >= 0
puts avg.round opts[:round]
else
puts avg
end
|