Sales Analysis: Average Price by Category

Python Medium dicts

Task «Sales Analysis: Average Price by Category»

Here is the translation of the programming task text from Russian to English:

A shopping list is given in the format 'category:product:price'. For each category, it is necessary to calculate the average price of products in that category (arithmetic mean of prices). Output the categories and their average prices, sorted by category name in alphabetical order. It is guaranteed that each category has at least one product.

Input Format

The input is an integer N (1 ≤ N ≤ 1000) — the number of records. Then N lines, each in the format 'category:product:price', where category and product are strings without spaces, and price is an integer from 1 to 100000. Categories may repeat.

Output Format

Output strings in the format 'category:average_price', where the average price is rounded to two decimal places (fixed-point format, use round or f-string). The strings are sorted by category in lexicographic order.

Examples

Example 1

INPUT
4 food:apple:100 food:banana:200 drink:juice:150 drink:water:50
OUTPUT
drink:100.00 food:150.00

Example 2

INPUT
2 clothes:shirt:500 clothes:pants:700
OUTPUT
clothes:600.00

Example 3

INPUT
3 toys:car:300 toys:doll:450 toys:ball:150
OUTPUT
toys:300.00
main.py