Group by with aggregate

Write codeMedium25 minSnowflake and Databricks style

Your task

Implement GROUP BY with an aggregate. Given rows (list of dicts), a group_col name, an agg_col name, and an agg function one of 'sum','count','avg', group rows by row[group_col] and compute the aggregate of row[agg_col] within each group. 'count' ignores agg_col and counts rows; 'avg' is the floating-point mean. Return a list of [group_value, aggregate] pairs sorted ascending by group_value. avg values must equal the exact float mean.

Examples

group_aggregate(rows: list[dict], group_col: str, agg_col: str, agg: str) -> list[list]
Example 1
Inputgroup_aggregate([{"dept": "eng", "sal": 100}, {"dept": "eng", "sal": 200}, {"dept": "hr", "sal": 50}], "dept", "sal", "sum")
Output[["eng", 300], ["hr", 50]]