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.
Function
parse_reviews(lines)Valid line
product_id,rating with exactly one comma, a non-empty ID (spaces stripped) and a whole-number rating from 1 to 5Returns
(list of (product_id, rating) tuples in order, number of rejected lines); blank lines are skipped without countingExamples
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)