#C1927. Calculate Total Inventory Value by Category
Calculate Total Inventory Value by Category
Calculate Total Inventory Value by Category
You are given an inventory list where each line represents an item record in the following format:
id, name, category, price
Your task is to compute the total price for the items in each category. There are exactly five categories, numbered 1 through 5. For each category k, the total value is calculated as
$$S_k = \sum_{\substack{\text{item}\ i \\ \text{if } category_i = k}} price_i$$
If a category does not appear in the list, its total value is considered to be 0.
The input will be provided via standard input (stdin) and the results must be printed to standard output (stdout). The output should contain five lines, each corresponding to a category from 1 to 5 in ascending order. Each line should have the format:
Total value in category k: X
where k
is the category number and X
is the total value (as a float if applicable).
inputFormat
The input consists of multiple lines. Each line contains a record with four fields separated by commas:
- id: an integer representing the item ID
- name: a string representing the item name
- category: an integer between 1 and 5
- price: a floating-point number representing the price
An empty input is also allowed. The input terminates at end-of-file.
outputFormat
The output should consist of exactly five lines. The i-th line (for 1 ≤ i ≤ 5) should have the format:
Total value in category i: X
where X
is the sum of prices for all items in category i
. If no items are present for a category, X
should be 0.
101, Laptop, 1, 1000
102, Chair, 2, 50
103, Phone, 1, 700
Total value in category 1: 1700.0
Total value in category 2: 50.0
Total value in category 3: 0
Total value in category 4: 0
Total value in category 5: 0
</p>