-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr5.py
37 lines (31 loc) · 961 Bytes
/
r5.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
34
35
36
37
# Micro-optimized r4 (read line at a time and parse with str.partition)
from dataclasses import dataclass
from line_profiler import profile
import sys
@dataclass
class Stat:
min: float
max: float
sum: float
count: float
@profile
def main():
floats = {str(i/10)+'\n': i/10 for i in range(-999, 1000)}
stats = {}
with open(sys.argv[1]) as f:
for line in f:
station, _, temp_str = line.partition(';')
temp = floats[temp_str]
try:
s = stats[station]
if temp < s.min:
s.min = temp
if temp > s.max:
s.max = temp
s.sum += temp
s.count += 1
except KeyError:
stats[station] = Stat(min=temp, max=temp, sum=temp, count=1)
for station, s in sorted(stats.items()):
print(f'{station}={s.min:.1f}/{s.sum/s.count:.1f}/{s.max:.1f}')
main()