← 返回 reddit 的题目列表BillingStatus Class with Transactions, Overwrite, Undo, and Redo
类型:online_judge
reddit
Problem Description
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 required to process the financial transactions from the old system to generate a BillingStatus instance for each user to be stored in our new system.
Part 1
Implement a class BillingStatus to represent an account state. Each financial transaction represents a modification to the BillingStatus. BillingStatus should be able to ingest new transactions generated in our systems.
Initialize the BillingStatus class with two monetary columns:
ad_delivery_pennies: 0
payment_pennies: 0
Every 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.
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},
...
}
Output:
{1: BillingStatus('ad_delivery_pennies'=3000, 'payment_pennies'=1000)}
Part 2
Introduce the concept of an "overwrite transaction", where the transaction can indicate whether it should overwrite the current BillingStatus monetary value.
Input:
monetary_columns = ('ad_delivery_pennies', 'payment_pennies')
transactions = {
'ff8ba98a-8d45-11e9-bc42-526af7764f64': {'user_id': 1, 'ad_delivery_pennies': 1000, 'transaction_timestamp': 1500000001, 'overwrite': False},
...
'ff923488-8d45-11e9-bc42-526af7764f64': {'user_id': 1, 'payment_pennies': 100, 'transaction_timestamp': 1500000013},
}
Output:
{1: BillingStatus('ad_delivery_pennies'=1000, 'payment_pennies'=600), 2: BillingStatus('ad_delivery_pennies'=4000, 'payment_pennies'=2600)}
Part 3
Add the concept of undo_last and redo_last. When undo_last is True, it will undo the previous transaction, and when redo_last is True, it will redo a previously undone transaction.
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, 'undo_last': True, 'transaction_timestamp': 1500000002},
...
}
Output:
{1: BillingStatus('ad_delivery_pennies'=1000, 'payment_pennies'=1000)}
Example
Input
{'ff8bc1c2-8d45-11e9-bc42-526af7764f64': {'user_id': 1, 'ad_delivery_pennies': 1000, 'transaction_timestamp': 1500000001}, 'ff8bc2e4-8d45-11e9-bc42-526af7764f64': {'user_id': 1, 'undo_last': True, 'transaction_timestamp': 1500000002}}