Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have encountered one problem statement recently. I am not sure answer about that question. I need answer for that. Can anybody explain in detail of that question and answer. Please suggest me.

I need answer in c#.

Please find the below link for the problem statement.
https://www.chegg.com/homework-help/questions-and-answers/top-product-given-list-product-sales-sale-consists-product-name-units-sold-next--upon-enco-q41518187#question-transcript

What I have tried:

Did not understand the problem statement. Please suggest me if you have an idea.
Posted
Updated 2-Dec-19 22:03pm
v2

We are not going to do your homework for you.

If you don't understand the question then you should discuss that with your tutor... or in this case the site experts.

If you make some effort we are more than happy to help you out, but homework is set to cement the knowledge into your brain and to allow your tutor to determine which areas they need to help you with.
 
Share this answer
 
Comments
Maciej Los 29-Nov-19 12:54pm    
5ed!
Sree(Member 12916318) 1-Dec-19 0:58am    
I got that question one of the online test. Nobody was not there to explain that.Now i understood that question by read couple of times. I will crack that to put some time. Thanks for the suggestion.
hint, take a look at String.Split() https://www.dotnetperls.com/split[^]
example:
string[] lines = data.Split(new[] { " top " }, StringSplitOptions.None);
 
Share this answer
 
v2
Comments
Sree(Member 12916318) 1-Dec-19 1:01am    
Thanks for the hint RickZeeland.I will look into it.
C#
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int t = int.Parse(Console.ReadLine());
            List<string> output = new List<string>();
            for (int i = 0; i < t; i++)
            {
                List<string> prodDetails = new List<string>();
                int c = int.Parse(Console.ReadLine());
                for (int j = 0; j < c; j++)
                {
                    prodDetails.Add(Console.ReadLine());
                }
                GetTopProductDetails(prodDetails, output);
            }
            output.ForEach(x => Console.WriteLine(x));
        }

        private static List<string> GetTopProductDetails(List<string> prodDetails, List<string> output)
        {
            Dictionary<string, int=""> keyValuePairs = new Dictionary<string, int="">();
            for (int i = 0; i < prodDetails.Count; i++)
            {
                if (prodDetails[i].ToLower() != "top")
                {
                    string[] x = prodDetails[i].Split(' ');
                    string P = x[0];
                    int U = int.Parse(x[1]);
                    if (keyValuePairs.ContainsKey(P))
                    {
                        int unit = 0;
                        keyValuePairs.TryGetValue(P, out unit);
                        keyValuePairs[P] = unit + U;
                    }
                    else
                    {
                        keyValuePairs.Add(P, U);
                    }
                }
                else
                {
                    var valuesList = keyValuePairs.Where(x => x.Value == keyValuePairs.Values.Max()).Select(v => v.Key).OrderBy(y => y).ToList();
                    string disp = string.Join(" ", valuesList);
                    output.Add(disp);
                }
            }
            return output;
        }
    }
}
 
Share this answer
 
v2
Comments
CHill60 3-Dec-19 5:47am    
An unexplained code dump is not a good solution, particularly as the OP stated that they didn't understand the question.
If this had been homework then you are helping no-one by just giving the code away without attempting to explain what it does.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900