runtime
393 / 500
question 393 Software Architecture · easy

A parser that reports what it rejected

Review exports contain bad lines. Write the parsing layer parse_reviews(lines): it receives the lines (already read by the caller, header removed) and returns (reviews, rejected): the valid reviews as (product_id, rating) tuples and the count of rejected lines. It does no file access and no printing.

Functionparse_reviews(lines)
Valid lineproduct_id,rating with exactly one comma, a non-empty ID (spaces stripped) and a whole-number rating from 1 to 5
Returns(list of (product_id, rating) tuples in order, number of rejected lines); blank lines are skipped without counting
Examples
callparse_reviews(['P0205,5', 'P0421, 4', 'P0205,nine', '', 'P0187,7'])returns([('P0205', 5), ('P0421', 4)], 2)
callparse_reviews([' P0001 ,1'])returns([('P0001', 1)], 0)
callparse_reviews([])returns([], 0)

Loading your progress…

A parser that reports what it rejected — Python practice · Runtime