blob: a9468b0089d1e105863ccdcc110f1adf9eec4620 (
plain)
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
|
#!/bin/env python3
# cost should be given in cents
def tanken(drives, cost):
passengers = {}
distance = 0.
drives = [d.split(' ') for d in drives]
for d in drives:
try:
d[0] = int(d[0])
except:
return None, "Lines have to start with the driven distance!"
# calculate overall distance
distance += d[0]
# collect distances per passenger
for p in d[1:]:
if p not in passengers:
passengers[p] = [d[0],0]
else:
passengers[p][0] += d[0]
# calculate cost per kilometer
c = cost/distance
for d in drives:
# calculate cost per drive split through passengers
c_d = int(c * d[0] / (len(d) - 1))
for p in d[1:]:
passengers[p][1] += c_d
return passengers, None
|