-
Notifications
You must be signed in to change notification settings - Fork 0
/
datamunging.py
40 lines (31 loc) · 1.51 KB
/
datamunging.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
# Part One: Weather Data
def getData(file, headers=None):
'''@file - input param "weather.dat" '''
f = open(file) # open a file to read Data
headersData = f.readline().split() # get headers from file as list
headers = headersData if headers is None else headers
return [dict(zip(headers, data.split())) for data in f.readlines()] # return data as list of dictionary with headers as keys
def getMinData(lst, func, minSpread=None):
'''Get min spread temp from lst by applying ftion 'f' '''
return (minSpread if lst == []
else( getMinData(lst[1:], func, lst[0]) if func(lst[0]) is not None
and (minSpread is None or func(lst[0]) < func(minSpread) )
else (getMinData(lst[1:], func, minSpread) )
)
)
def weatherData():
'''@lambda function is used to get temp spread for valid rows'''
file = 'weather.dat' # file where weather data present
data = getData(file)
day = getMinData(data, lambda temp: abs(float(temp['MxT'].replace('*','')) - float(temp['MnT'].replace('*','')))
if 'Dy' in temp else None)
print('The required day Number is - {}'.format(day['Dy']))
def soccerLeague():
'''@lambda function to get the difference in "for" and "against" goals'''
file = 'football.dat' # file where all football points data present
data = getData(file, headers=['Nr','Team','P','W','L','D','F','-','A','Pts'])
team = getMinData(data, lambda t: abs(int(t['F']) - int(t['A'])) if 'Team' in t else None)
print('Name of the team is - {}'.format(team['Team']))
# start of program
weatherData()
soccerLeague()