#C13595. CSV Sales Data Processing
CSV Sales Data Processing
CSV Sales Data Processing
You are given CSV data via standard input that contains sales records. The first row is the header with columns: SaleID
, ProductCategory
, and SaleAmount
. Each subsequent row represents a sales record.
Your task is to read the CSV data and perform the following steps:
- Count the total number of records (excluding the header).
- Filter out the records where the sale amount is 500\)).
- Group the filtered records by
ProductCategory
and compute the total sales amount in each category. - Determine the top 3 categories with the highest total sales amount (sorted in descending order).
Finally, output the result in JSON format with the following keys:
total_records
: Total number of records processed.records_gt_500
: Number of records with a sale amount > 500.category_sales
: A JSON object where keys are product categories and values are the total sales amount for that category.top_3_categories
: An array of arrays, where each sub-array contains the category name and its total sales amount, for the top 3 categories.
inputFormat
Input is given via standard input (stdin). The input consists of multiple lines. The first line is the header: SaleID,ProductCategory,SaleAmount
. Each following line represents a record with three fields separated by commas.
outputFormat
The output should be printed to standard output (stdout) in JSON format, containing the following structure:
{ "total_records": number, "records_gt_500": number, "category_sales": { "CategoryName": amount, ... }, "top_3_categories": [["CategoryName", amount], ...] }## sample
SaleID,ProductCategory,SaleAmount
1,Electronics,150.00
2,Furniture,550.25
3,Electronics,660.50
4,Clothing,780.00
5,Furniture,400.00
6,Furniture,600.75
7,Electronics,900.00
8,Clothing,1020.00
9,Clothing,480.00
10,Electronics,510.00
{"total_records":10,"records_gt_500":7,"category_sales":{"Furniture":1151.0,"Electronics":2070.5,"Clothing":1800.0},"top_3_categories":[["Electronics",2070.5],["Clothing",1800.0],["Furniture",1151.0]]}