Sobes.tech
Junior — Senior

Determination of deviations in sums by clients

livecode

Task condition

There are two sorted arrays of dictionaries list1 and list2. Each element contains a client identifier (client_id) and their financial amount (sum). It is necessary to match records from both arrays by the client_id field, calculate the absolute difference of sums for each client, and sum all deviations. After that, a list of the ten clients with the largest differences in absolute value should be formed.

list1 = [
    {'client_id': 1, 'sum': 10},
    {'client_id': 2, 'sum': 20},
    {'client_id': 3, 'sum': 40},
]

list2 = [
    {'client_id': 1, 'sum': 10},
    {'client_id': 2, 'sum': 25},
    {'client_id': 3, 'sum': 30},
]

# Expected result:
# total deviation sum: 5 + 40 + 30 = 75
# top10:
 top10 = [
    {'client_id': 3, 'diff': 40},
    {'client_id': 2, 'diff': 5},
    {'client_id': 1, 'diff': 0},
]
list1 = [
    {'client_id': 1, 'sum': 10},
    {'client_id': 2, 'sum': 20},
    {'client_id': 3, 'sum': 40},
]

list2 = [
    {'client_id': 1, 'sum': 10},
    {'client_id': 2, 'sum': 25},
    {'client_id': 3, 'sum': 30},
]