In the Ancestry Marketing mobile app, analytics events arrive as pipe-delimited strings. Implement a function that both parses these events and applies business rules to compute the total revenue from valid purchase events.
Each event string has the format:
timestamp|event_type|user_id|campaign|amount
Your task is to process a list of event strings and return the sum of amounts for valid purchase events.
event_type == "purchase" count toward revenue.timestamp or amount is not a valid integer.amount <= 0.(timestamp, user_id). If the same purchase appears multiple times, count it only once.events, a list of stringsExample 1
Input: events = ["100|purchase|u1|summer|20", "101|open|u1|summer|0", "100|purchase|u1|summer|20"]
Output: 20
Explanation: Only purchase events count, and the duplicate purchase at timestamp 100 for user u1 is counted once.
Example 2
Input: events = ["200|purchase|u2|dna|15", "bad|purchase|u3|dna|10", "201|purchase|u4|dna|-5"]
Output: 15
Explanation: The second record has an invalid timestamp, and the third has a non-positive amount.
1 <= len(events) <= 10^41 and 200timestamp and amount, when valid, fit in 32-bit signed integersevents = ["100|purchase|u1|summer|20", "101|open|u1|summer|0", "100|purchase|u1|summer|20"]Output20WhyOnly the first and third records are purchases, but they represent the same `(timestamp, user_id)` purchase, so revenue is counted once.events = ["200|purchase|u2|dna|15", "bad|purchase|u3|dna|10", "201|purchase|u4|dna|-5"]Output15WhyThe first record is valid. The second has a non-integer timestamp, and the third has a non-positive amount.1 <= len(events) <= 10^41 <= len(events[i]) <= 200Each valid record has exactly 5 pipe-delimited fieldstimestamp and amount, when valid, fit in 32-bit signed integersdef total_valid_purchase_revenue(events):