Level 1 Filter transactions
A wallet service keeps a list of transactions and needs a small query layer over it. Every record is a list of five strings:
[timestamp, id, user_id, currency, amount]
["1700000000", "tx9", "u1", "BTC", "0.25"]
timestamp is a whole number of seconds, amount is a non-negative decimal such as "12" or "0.25", and ids are unique. Compare timestamps and amounts as numbers, not as strings ("9" < "10", and "2.5" == "2.50").
Build TransactionLog:
TransactionLog(records: list[list[str]])query(filters: dict | None = None) -> list[str]: the ids of every record that passes all the given filters, ordered by timestamp, then by id (string order) for equal timestamps.
Every filter key is optional; None or {} means "no filters":
| key | keeps records with |
|---|---|
"start_time" |
timestamp >= value |
"end_time" |
timestamp <= value |
"min_amount" |
amount >= value |
"max_amount" |
amount <= value |
"user_id" |
user_id == value |
"currency" |
currency == value |
Time values are ints; amount values are decimal strings like the records' own. Any other key raises ValueError.
log = TransactionLog([
["30", "c", "u1", "ETH", "5"],
["10", "b", "u2", "BTC", "0.5"],
["10", "a", "u1", "BTC", "12.00"],
["20", "d", "u1", "BTC", "3"],
])
log.query() # ["a", "b", "d", "c"]
log.query({"user_id": "u1", "currency": "BTC"}) # ["a", "d"]
log.query({"min_amount": "3", "end_time": 20}) # ["a", "d"]
log.query({"max_amount": "0.50"}) # ["b"]
log.query({"colour": "red"}) # ValueError
Show hint
Parse each record once in the constructor (int for the timestamp, decimal.Decimal for the amount) and keep them sorted by (timestamp, id); then a query is a single filtered pass.