vendredi 8 septembre 2023

GetTopStocks(stocks, prices)

 In this problem, you'll be given a list of daily stock prices, and you'll be asked to return the three stocks

with the highest average price.

Implementation

Implement the méthode GetTopStocks(stocks, prices) which takes as input: un tableau of strings

( stocks ), representing the considered stocks. un tableau of 2 dimensions ( prices ), representing the

stock prices (inner lists) for each day (outer list).



using System;

using System.Collections.Generic;

using System.Linq;

public class Program

{

public static void Main()

{

string[] items = {"A","B","C","D","E"};

float[,] prices =  new float [3,5] {{9,2,2,4,1},{4,2,1,3,1},{2,3,2,1,1}};

Dictionary<string, float> dict = new Dictionary<string, float>();

for(int i = 0; i < items.Length; i++){

float total = 0;

for(int j = 0; j < prices.GetLength(0); j++){

total = total + prices[j,i];

}

dict.Add(items[i], total/prices.GetLength(0));

}

var newDict = new string[3]; //result to return

for(int i = 0; i<3; i++){

newDict[i] = dict.OrderByDescending(x => x.Value).First().Key;

dict.Remove(newDict[i]);

}

                

foreach(var item in newDict){

Console.WriteLine(item);

}

}

}

Aucun commentaire:

Enregistrer un commentaire