We accidentally dropped the database where we store the current billing status for our advertisers. Fortunately, we still have the logs for all the transactions they did, and we can use this to recreate the dropped data.
You are asked to process the financial transactions from the old system to build up a BillingStatus per user to be stored in our new system.
You should create a class called BillingStatus, which will represent an account state. Each financial transaction represents a modification to a BillingStatus. A BillingStatus should be able to ingest new transactions that are generated in our own systems.
Given a collection of financial transactions, we want to generate a BillingStatus instance for each user. This can be represented as a dict:
{user_id: BillingStatus(), user_id2: BillingStatus()}
Our BillingStatus class should start with two monetary columns:
'ad_delivery_pennies': 0, 'payment_pennies': 0
Each transaction can have multiple monetary columns. Upon processing a transaction, the values in the monetary columns should be added to the current value in the BillingStatus.
Given this input:
monetary_columns = ('ad_delivery_pennies', 'payment_pennies')
transactions = {'ff8bc1c2-8d45-11e9-bc42-526af7764f64': {'user_id': 1, 'ad_delivery_pennies': 1000, 'transaction_timestamp': 1500000001},
'ff8bc2e4-8d45-11e9-bc42-526af7764f64': {'user_id': 1, 'ad_delivery_pennies': 1000, 'transaction_timestamp': 1500000002},
'ff8bc4ec-8d45-11e9-bc42-526af7764f64': {'user_id': 1, 'payment_pennies': 500, 'transaction_timestamp': 1500000003},
'fv24z4ec-8d45-11e9-bc42-526af7764f64': {'user_id': 1, 'ad_delivery_pennies': 1000, 'payment_pennies': 500, 'transaction_timestamp': 1500000004}
}
Expected Output (format however you want):
{1: BillingStatus({'ad_delivery_pennies': 3000, 'payment_pennies': 1000})}
# INSERT YOUR CODE BELOW THIS LINE
This problem asks you to rebuild per-user billing state by aggregating transaction logs. The key idea is to use a hash map from user_id to a BillingStatus object, then update only the monetary fields listed in monetary_columns for each transaction. Non-monetary metadata such as transaction_timestamp should be ignored. A clean solution usually initializes each BillingStatus with zero values for all tracked columns and adds amounts incrementally as transactions are processed.