You are given a small in-memory table of data and are asked to implement a simplified pivot table operation and a printer for it.
Assume the input data is represented as a list of rows, where each row is a mapping from column name (string) to value. For example, in a typical language this could be:
List<Map<String, Object>> rows
in Java, or
vector<unordered_map<string, Value>> rows
in C++, or
List<Dict[str, Any]]
in Python.
Each row contains several categorical columns (strings/integers) and at least one numeric column (integer/float). You are not required to design the exact type signatures; focus on the core logic and data structures.
You need to implement the following functionality in three steps:
Implement a function that takes:
rows
: the list of input rows,
c1
,
c2
,
c3
.
Exactly one of these three columns is numeric (the value column); the other two are categorical (the row axis and column axis). You may assume the caller tells you which is which, or you may infer it if that’s convenient; just make it clear in your API.
Your task:
(row_key, col_key)
of those two categorical values, compute the
sum
of the numeric column over all input rows that match that pair.
row_key -> col_key -> sum
).
You may choose any reasonable text layout as long as it is consistent and human-readable.
Example (conceptual)
Suppose the raw data has columns "city", "product", and "sales".
If we choose:
"city"
,
"product"
,
"sales"
,
then the cell at (city = "SF", product = "Book") contains the sum of sales for all rows where city == "SF" and product == "Book".
Extend your pivot table implementation so that the printed table also includes:
Update your printing function to show these totals clearly.
Finally, generalize the pivot table so that each axis can be defined by multiple columns, rather than exactly one.
Modify your function signature so that it takes:
row_key_columns
: a list of column names to be used (together) as the row axis key,
col_key_columns
: a list of column names to be used (together) as the column axis key,
value_column
: the single numeric column to aggregate.
Requirements:
row_key_columns
.
col_key_columns
.
You should:
(row_key, col_key)
pair) by outputting 0 for that cell.
Do not worry about file I/O, UI, or formatting beyond a simple, text-based table printout.