Comprehensions Over Records
mediumpy_datastructuresTen little extractors over a hit-log: each record is a dict with EventID,
EventTime, UserID, PageID, RegionID (may be None), DeviceType.
Write each as a single comprehension — no for/while loops and no
map/filter:
get_unique_page_ids— set of allPageIDs.get_unique_page_ids_visited_after_ts(records, ts)—PageIDs withEventTime > ts.get_unique_user_ids_visited_page_after_ts(records, ts, page_id)—UserIDs that hitpage_idafterts.get_events_by_device_type(records, device_type)— records with that device (order preserved).get_region_ids_with_none_replaces_by_default(records)— eachRegionID, withNone→DEFAULT_REGION_ID(100500).get_region_id_if_not_none(records)— the non-NoneRegionIDs.get_keys_where_value_is_not_none(r)— keys of a record with a non-Nonevalue.get_record_with_none_if_key_not_in_keys(r, keys)— same record, values whose key isn't inkeysset toNone.get_record_with_key_in_keys(r, keys)— the record restricted tokeys.get_keys_if_key_in_keys(r, keys)— the record's keys that are inkeys.
Constraints
- no map, filter
- no for/while loops
Your solution
Edit get_unique_page_ids and run the real pytest suite in your browser — no install required. Your code is saved locally.
Loading the Python runtime (first run only)…
Hints
Hint 1
Each function is one list/set/dict comprehension over the records — e.g. {r['PageID'] for r in records}.
Hint 2
Add an `if` clause to filter; use a conditional expression (a if cond else b) for the default-value cases.