#C13256. CSV Product Data Analysis

    ID: 42774 Type: Default 1000ms 256MiB

CSV Product Data Analysis

CSV Product Data Analysis

This problem requires you to analyze product data provided in CSV format. The CSV input is read from standard input and contains the following columns:

  • product_id
  • category
  • price
  • quantity_sold
  • rating

Your task is to compute and output a JSON object with four keys:

  • total_products: the total number of product records.
  • average_rating: the average rating computed as \(\text{average} = \frac{\text{sum of ratings}}{\text{total products}}\). If there are no products, the average rating is 0.
  • highest_priced_product: an object representing the product with the highest price. This object should include product_id, category, price, quantity_sold, and rating. If no product exists, output an empty object.
  • total_quantity_sold_per_category: an object that maps each category to the total quantity sold in that category.

Note: If the input only contains the header line (or is empty), your output must be:

{"total_products":0,"average_rating":0,"highest_priced_product":{},"total_quantity_sold_per_category":{}}

inputFormat

The input is provided through standard input (stdin) in CSV format. The first line is a header line:

product_id,category,price,quantity_sold,rating

Each subsequent line represents a product record with comma-separated values. You can assume that the input is well-formed, although it may be empty (apart from the header) which should be handled appropriately.

outputFormat

Output a single JSON object to standard output (stdout) with the following keys:

  • total_products: An integer representing the total number of products.
  • average_rating: A floating point number representing the average rating of the products.
  • highest_priced_product: A JSON object representing the product with the highest price. It includes keys: product_id, category, price, quantity_sold, and rating. If no product exists, output an empty object.
  • total_quantity_sold_per_category: A JSON object mapping each category to the sum of quantity_sold for that category.
## sample
product_id,category,price,quantity_sold,rating
1,Electronics,299.99,150,4.5
2,Books,19.99,1200,4.8
3,Clothing,49.99,500,4.2
4,Electronics,399.99,100,4.6
{"total_products":4,"average_rating":4.525,"highest_priced_product":{"product_id":4,"category":"Electronics","price":399.99,"quantity_sold":100,"rating":4.6},"total_quantity_sold_per_category":{"Electronics":250,"Books":1200,"Clothing":500}}