Pivot sparse records
Your task
Given a list of records, each a dict with keys 'row', 'col', and 'val' (int), pivot them into a dense matrix. Collect the distinct 'row' values and distinct 'col' values, sort each ascending, and build a 2D list where entry [i][j] is the sum of all 'val' for records whose row equals the i-th sorted row label and whose col equals the j-th sorted col label. Cells with no record are 0. Return a dict with keys 'rows' (sorted row labels), 'cols' (sorted col labels), and 'matrix' (the 2D list). The input may be empty (return empty lists for all three).
Examples
pivot_sum(records: list[dict]) -> dictExample 1
Input
pivot_sum([{"row": "x", "col": "a", "val": 1}, {"row": "x", "col": "b", "val": 2}, {"row": "y", "col": "a", "val": 3}])Output
{"rows": ["x", "y"], "cols": ["a", "b"], "matrix": [[1, 2], [3, 0]]}