aboutsummaryrefslogtreecommitdiff
path: root/tanken.py
blob: 2ca80bd8291b5278811737089273f29b81408597 (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
35
36
#!/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
    if distance <= 0:
        return None, "Driven distance must be greater than 0!"
    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