-
Notifications
You must be signed in to change notification settings - Fork 0
/
station_search.py
104 lines (70 loc) · 2.84 KB
/
station_search.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
class StationSearch:
def __init__(self, filename='./data.csv'):
self.filename = filename
def min_temperature(self) -> (str, str):
"""
Returns the station_id and date associated with the
lowest temperature on record.
:return: station_id, date
"""
with open(self.filename, 'r', newline='', encoding='utf-8') as f:
f.readline()
min_temp = 9999.9
out = (None, None)
for line in f:
# Remove carriage return from line, extract data to tuple
d = line[:-2].split(',')
temp = float(d[2])
if temp < min_temp:
min_temp = temp
out = (d[0], d[1])
return out[0], out[1]
def max_fluctuation(self) -> str:
"""
Returns the station_id that recorded the largest fluctuations.
:return: station_id
"""
with open(self.filename, 'r', newline='', encoding='utf-8') as f:
record = {}
max_fluc = -9999
max_id = None
f.readline()
for line in f:
d = line[:-2].split(',')
station_id = d[0]
new_temp = float(d[2])
if station_id not in record:
record[station_id] = (0, new_temp)
else:
prev_fluc, prev_temp = record[station_id]
new_fluc = prev_fluc + abs(new_temp - prev_temp)
record[station_id] = (new_fluc, new_temp)
if new_fluc > max_fluc:
max_fluc = new_fluc
max_id = station_id
return max_id
def max_fluctuation_by_dates(self, start: str, end: str) -> str:
"""
Returns the station_id that recorded the largest fluctuations
within the provided date range.
:return: station_id
"""
with open(self.filename, 'r', newline='', encoding='utf-8') as f:
record = {}
max_fluc = -9999
max_id = None
f.readline()
for line in f:
d = line[:-2].split(',')
station_id = d[0]
new_temp = float(d[2])
if station_id not in record:
record[station_id] = (0, new_temp)
elif start <= d[1] <= end:
prev_fluc, prev_temp = record[station_id]
new_fluc = prev_fluc + abs(new_temp - prev_temp)
record[station_id] = (new_fluc, new_temp)
if new_fluc > max_fluc:
max_fluc = new_fluc
max_id = station_id
return max_id