-
Notifications
You must be signed in to change notification settings - Fork 0
/
average.py
34 lines (25 loc) · 820 Bytes
/
average.py
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
32
33
#-----------------------------------------------------------------------
# average.py
#-----------------------------------------------------------------------
import stdlib.stdio as stdio
# Read floats from the standard input stream until end-of-file.
# Write to standard output the average of those floats.
total = 0.0
count = 0
while not stdio.isEmpty():
value = stdio.readFloat()
total += value
count += 1
avg = total / count
stdio.writeln('Average is ' + str(avg))
#-----------------------------------------------------------------------
# python3 average.py
# 10.0 5.0 6.0
# 3.0
# 7.0 32.0
# Average is 10.5
# python3 randomseq.py 1000 > data.txt
# python3 average.py < data.txt
# Average is 0.49134854771784825
# python3 randomseq.py 1000 | python3 average.py
# Average is 0.49712655575298226