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:
user_id, event, or value.event by trimming whitespace and converting to lowercase.event is one of "click", "view", or "purchase".value to an integer if possible; otherwise ignore the record.user_id.For each user, return:
user_idtotal_value: sum of valid valuesevent_counts: counts for each valid event type that appearedReturn the final result as a list of dictionaries sorted by:
total_value descendinguser_id ascendingrecords, a list of dictionariesExample 1
Input: records = [{"user_id":"u1","event":" Click ","value":"3"},{"user_id":"u1","event":"view","value":2},{"user_id":"u2","event":"purchase","value":"5"}]
Output: [{"user_id":"u1","total_value":5,"event_counts":{"click":1,"view":1}},{"user_id":"u2","total_value":5,"event_counts":{"purchase":1}}]
Explanation: Both users total 5, so u1 comes first because of ascending user_id.
Example 2
Input: records = [{"user_id":"u1","event":"signup","value":4},{"user_id":"u2","event":"view","value":"bad"}]
Output: []
Explanation: Both records are invalid after filtering.
1 <= len(records) <= 10^4value may be an integer or a numeric stringuser_id is compared lexicographicallyrecords = [{"user_id":"u1","event":" Click ","value":"3"},{"user_id":"u1","event":"view","value":2},{"user_id":"u2","event":"purchase","value":"5"}]Output[{"user_id":"u1","total_value":5,"event_counts":{"click":1,"view":1}},{"user_id":"u2","total_value":5,"event_counts":{"purchase":1}}]WhyAfter normalization and conversion, all three records are valid. Both users have total value 5, so the tie is broken by ascending `user_id`.records = [{"user_id":"u1","event":"signup","value":4},{"user_id":"u2","event":"view","value":"bad"}]Output[]WhyThe first record has an unsupported event and the second has a non-numeric value, so no valid records remain.1 <= len(records) <= 10^4Each record is a dictionary with string keys`event` may contain extra whitespace or mixed case`value` may be an integer or a numeric stringOnly `click`, `view`, and `purchase` are valid eventsdef summarize_records(records):