Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
The finance team sends a monthly spreadsheet, and the header row is written by hand. It has spaces, punctuation, inconsistent casing, a column whose name starts with a year, and — because three people maintain it — two headers that mean the same thing spelled two ways.
Write clean_column_names(names). Return one safe column name per header, in the same order.
Function to write
clean_column_names(names: list) -> list[str]One cleaned, unique snake_case name per header, in the original order.
How to approach it
Clean one name in a helper; the uniqueness rule needs a count of what you have already emitted.
Sample cases
+ 2 held back until you submit
a spreadsheet export
Spaces, padding, punctuation, a leading digit, three headers that collide, and one that is only punctuation.
Input
Argument 1
[
'Order ID',
' Customer Name ',
'Order-ID',
'2024 Total',
'Ship To / Country',
'order id',
'%'
]Returns
[
'order_id',
'customer_name',
'order_id_2',
'col_2024_total',
'ship_to_country',
'order_id_3',
'column'
]no headers
An empty header row produces an empty list rather than a list holding one empty name.
Input
Argument 1
[] (empty list)
Returns
[] (empty list)
already clean
Names that are already valid are returned untouched, with no suffix added.
Input
Argument 1
[
'order_id',
'customer_name'
]Returns
[
'order_id',
'customer_name'
]Constraints
col_.column.total becomes total_2, the third total_3.Worked example
Order ID, Order-ID and order id are three different headers that all clean down to order_id. They come back as order_id, order_id_2 and order_id_3 — renaming rather than dropping, because a dropped column shifts every column after it and the file silently loads into the wrong fields.
2024 Total becomes col_2024_total, and % cleans down to nothing at all and becomes column. Neither is a good column name and both are better than the alternative, which is a load that fails at three in the morning on a file that looked fine in the spreadsheet.
What this tests
String normalisation with a rule you can state, and the discipline of keeping position stable. Uniqueness is the part people skip, and it is the part that turns a clean-looking header row into data in the wrong columns.
clean_column_names(names: list) -> list[str]Submit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.