#C13565. Sales Data Analysis
Sales Data Analysis
Sales Data Analysis
You are given the content of a CSV file via standard input. The CSV file contains sales records, where each record represents a single transaction. The CSV file is expected to have at least two columns: Product Category and Sales.
Your task is to compute two things:
- The total sales per product category. In other words, for each unique product category, sum all the sales.
- The average sales per transaction, which is computed by \(\text{average} = \frac{\text{total sales}}{\text{number of transactions}}\).
If the CSV file does not contain both required columns or if the input is empty, your program should print an appropriate error message.
Note: The input is provided via standard input (stdin) and the output should be printed to standard output (stdout) as a JSON string with the following format:
{ "total_sales_per_category": { ... }, "average_sales_per_transaction": value }
inputFormat
The input is provided via standard input and consists of CSV file content. The first line is the header which should include at least the following columns: Product Category
and Sales
. Each subsequent line represents a transaction with values separated by commas.
For example:
Product Category,Sales Electronics,200 Electronics,300 Groceries,100 Groceries,150
outputFormat
If the input is valid, output a JSON string representing a dictionary with two keys:
total_sales_per_category
: an object where each key is a product category and its value is the total sales for that category.average_sales_per_transaction
: a number representing the average sale per transaction.
If the CSV is missing required columns, or if the input is empty, print the corresponding error message.
## sampleProduct Category,Sales
Electronics,200
Electronics,300
Groceries,100
Groceries,150
{"total_sales_per_category": {"Electronics": 500, "Groceries": 250}, "average_sales_per_transaction": 187.5}