How to solve this question?

Given a list of pairs . Each pair contains a food item and its price, for ex: (apple, 120), (banana,200)… and so on. Specific food item may occur multiple times, for ex: (apple,200), (apple,280)… so on.

For each food item output the lowest, highest and average price(rounded down to nearest integer) for that item. The output list should be lexicographically (alphabetically) sorted by name of food.

Input:

first line contains one integer T i.e. no of test cases.

second line contains one integer N i.e. no of pair of fruits

next N line contains pair of fruits.

Output:

Output of result should be in following format:

case #i:

where i is the index of text case, starting from 1. Then for each test case output is one line format :

food item, its lowest price, its highest price, its average price

and output list of food items must be sorted item name in lexicographically order.

Sample Input:

3

3

banana 90

apple 100

apple 260

5

grapefruit 120

grape 200

grapefruit 150

grapefruit 150

grape 180

2

apple 100

apple 101

Sample Output

Case #1:

apple 100 260 180

banana 90 90 90

Case #2:

grape 180 200 190

grapefruit 120 150 140

Case #3:

apple 100 101 100

Hi there, and welcome to the forums!

You can do that manually, can’t you? As in, for each of the test cases you could - using a pen and paper - work out which food items you have, what the cheapest cost is, what the highest cost is, and what the average is if there’s more than one.

If you can do it by hand, you understand what the process is for solving the problem. Write out that process as a series of steps - each step should be a single action, ideally - and you’ll find that that sequence of actions looks surprisingly like the objects (variables) and flow structures (loops, if/else) you’ve encountered in code.

At that point, all you need to do is take your “manual” algorithm - the steps you’ve written out - and convert it into a program. :slight_smile:

2 Likes