runtime
119 / 500
question 119 Collection Algorithms · hard

Merge two customer exports

Two exports of the customer table overlap: a customer can appear in both, or twice in one, with a different country. Each record carries a version number that goes up whenever the record is edited. Merge them into one list with a single record per customer.

Functionmerge_customers(old, new)
old, newlists of dictionaries with customer_id, country and version
Winnerfor each customer, the record with the highest version; on equal versions the record that comes later (old is read before new, each top to bottom)
Ordercustomers appear in the order they are first seen (old first, then new), even when a later record wins
Must notchange either input list or any record in them
Examples
callmerge_customers([{'customer_id': '18102', 'country': 'United Kingdom', 'version': 1}, {'customer_id': '13085', 'country': 'United Kingdom', 'version': 2}, {'customer_id': '12682', 'country': 'France', 'version': 1}], [{'customer_id': '13085', 'country': 'Ireland', 'version': 1}, {'customer_id': '12682', 'country': 'Belgium', 'version': 3}, {'customer_id': '16329', 'country': 'Germany', 'version': 1}])returns[{'customer_id': '18102', 'country': 'United Kingdom', 'version': 1}, {'customer_id': '13085', 'country': 'United Kingdom', 'version': 2}, {'customer_id': '12682', 'country': 'Belgium', 'version': 3}, {'customer_id': '16329', 'country': 'Germany', 'version': 1}]
callmerge_customers([], [{'customer_id': '13085', 'country': 'Ireland', 'version': 1}, {'customer_id': '12682', 'country': 'Belgium', 'version': 3}, {'customer_id': '16329', 'country': 'Germany', 'version': 1}])returns[{'customer_id': '13085', 'country': 'Ireland', 'version': 1}, {'customer_id': '12682', 'country': 'Belgium', 'version': 3}, {'customer_id': '16329', 'country': 'Germany', 'version': 1}]
callmerge_customers([{'customer_id': '18102', 'country': 'United Kingdom', 'version': 1}, {'customer_id': '13085', 'country': 'United Kingdom', 'version': 2}, {'customer_id': '12682', 'country': 'France', 'version': 1}], [])returns[{'customer_id': '18102', 'country': 'United Kingdom', 'version': 1}, {'customer_id': '13085', 'country': 'United Kingdom', 'version': 2}, {'customer_id': '12682', 'country': 'France', 'version': 1}]

Loading your progress…

Merge two customer exports — Python practice · Runtime