← 返回 stripe 的题目列表CSV Parsing and Processing
类型:online_judge
Given a CSV-formatted string where each line represents a record and fields are separated by commas, implement a function to perform the following operations:
Calculate the maximum, minimum, and average of a specified numerical field.
Output the CSV data sorted by a specified field.
Input: The string csv_content, field name field_name, and operation type operation.
Output: Return the result based on the operation. If operation is 'max' or 'min', return the maximum or minimum value of the specified field. If operation is 'avg', return the average value of the field (rounded to two decimal places). If operation is 'sort', return the entire CSV content sorted by the specified field.
Notes:
The first line is the CSV header.
Assume the given field name exists.
Numerical fields may contain empty values; these should be ignored.
The strings may contain extra spaces; these should be trimmed.
Example:
name,age,score
Alice,30,85
Bob,25,90
Charlie,35,78
For operation='max' and field_name='age', the output should be 35.
For operation='sort' and field_name='score', the output should be:
name,age,score
Bob,25,90
Alice,30,85
Charlie,35,78
Example
Input
"name,age,score\nAlice,30,85\nBob,25,90\nCharlie,35,78\n", 'age', 'max'