-
Notifications
You must be signed in to change notification settings - Fork 0
/
citytripstatisticcalculator.py
46 lines (35 loc) · 1.59 KB
/
citytripstatisticcalculator.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
class StatisticsCalculator:
def __init__(self):
self.totdist = 0
self.userdist = {}
self.citycount = {}
def process(self,line):
acct, city1, city2, dist = line.split(":")[0],line.split(":")[1],line.split(":")[2],line.split(":")[3]
self.totdist += int(dist)
if(acct not in self.userdist):
self.userdist[acct] = int(dist)
else:
self.userdist[acct] += int(dist)
if(city1 not in self.citycount):
self.citycount[city1] = 1
else:
self.citycount[city1] += 1
if(city2 not in self.citycount):
self.citycount[city2] = 1
else:
self.citycount[city2] += 1
#print(sorted(self.userdist.items(),reverse=True, key=lambda item: item[1]))
maxvisits = sorted(self.citycount.items(),reverse=True, key=lambda item: item[1])[0][1]
topcity = []
for i in sorted(self.citycount.items(),reverse=True, key=lambda item: item[1]):
if i[1] == maxvisits:
topcity.append(i[0])
topcity.sort()
maxdist = sorted(self.userdist.items(),reverse=True, key=lambda item: item[1])[0][1]
topaccts = []
for i in sorted(self.userdist.items(),reverse=True, key=lambda item: item[1]):
if i[1] == maxdist:
topaccts.append(i[0])
topaccts.sort()
line = str(self.totdist) +":" +str(topaccts[0]) + ":" + str(topcity[0])
return line;