← 返回 waymo 的题目列表Parse CSV String Into a Usable Data Structure (with Corruption Handling)
类型:online_judge
Problem: Parse a CSV String into a Usable Data Structure (with Corruption Handling)
Given a CSV-formatted string s that may be clean or corrupted, parse it into a data structure convenient for downstream use.
Input
A string s representing CSV content.
The first line is the header containing column names and is guaranteed to be correct.
Lines are separated by newline characters (e.g., \n).
Fields in a line are separated by commas ,.
Example input:
col1,col2,col3,col4
value1,value2,value3,value4
Output (required return structure)
Return a row-oriented structure:
Return a map/dictionary rows:
key: row index starting from 0 (0 is the first data row, header excluded)
value: a dictionary containing:
mappings from column name to the row's field value: {col_name: value}
an extra boolean field is_valid indicating whether the row is valid
Validity rules (corruption handling)
For each data row:
If the number of fields equals the number of header columns, set is_valid = true
If the number of fields is less or more than the number of header columns, set is_valid = false
When counts mismatch:
Still align and fill what you can (e.g., fill the first min(k, m) columns in order)
For missing columns, fill with empty string "" (or None, choose one consistently)
For extra fields, either ignore them or store them in an extra field (e.g., extra_fields), choose one consistently
Constraints / Requirements
Do not infer/validate data types; treat all values as strings.
Aim for linear-time processing and state the time complexity.
Sample tests
Test 1: Clean data
Input:
col1,col2,col3
v1,v2,v3
Expected:
Row 0 is valid
Test 2: Missing fields
Input:
c1,c2,c3
x,y
Expected:
Row 0 is invalid
Test 3: Extra fields
Input:
c1,c2
x,y,z
Expected:
Row 0 is invalid
Test 4: Mixed rows
Input:
a,b,c
1,2,3
4,5
6,7,8,9
Expected:
Row 0 valid
Row 1 invalid (missing fields)
Row 2 invalid (extra fields)
Test 5: Header only
Input:
c1,c2,c3
Expected:
Return an empty map/list
Example
Input
col1,col2,col3
v1,v2,v3
Output
rows[0].is_valid == true