Problem
A data team at Acme Analytics receives raw event records as Python dictionaries. Implement a function that cleans these records and returns a per-user summary suitable for analysis.
Given a list of event dictionaries, process each record as follows:
- Ignore records missing
user_id,event, orvalue. - Normalize
eventby trimming whitespace and converting to lowercase. - Keep only records where normalized
eventis one of"click","view", or"purchase". - Convert
valueto an integer if possible; otherwise ignore the record. - Aggregate valid records by
user_id.
For each user, return:
user_idtotal_value: sum of valid valuesevent_counts: counts for each valid event type that appeared
Return the final result as a list of dictionaries sorted by:
total_valuedescendinguser_idascending
Constraints
- 1 <= len(records) <= 10^4
- Each record is a dictionary with string keys
eventmay contain extra whitespace or mixed casevaluemay be an integer or a numeric string- Only
click,view, andpurchaseare valid events
Function Signature
def summarize_records(records):
You are practicing as a guest. Sign up free to run your code against the sample data. Your draft stays right here.
