Coffee Time exercise from c# dictionary, hint 🤔 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Coffee Time exercise from c# dictionary, hint 🤔

Hey i see that this quest have hint "use coffee.Keys.ToArray() inside foreach loop" but i dont understand why. I tried to solve this quest and i pass but i put coffe.Keys.ToArray() outside foreach loop how is possible to use it inside? int discount = Convert.ToInt32(Console.ReadLine()); Dictionary<string, int> coffee = new Dictionary<string, int>(); coffee.Add("Americano", 50); coffee.Add("Latte", 70); coffee.Add("Flat White", 60); coffee.Add("Espresso", 60); coffee.Add("Cappuccino", 80); coffee.Add("Mocha", 90); string[] coffeName = coffee.Keys.ToArray(); foreach(string key in coffeName) { Console.WriteLine("{0} : {1}",key,Math.Round(coffee[key]*(Convert.ToDouble(100-discount)/100),MidpointRounding.AwayFromZero)); } Console.ReadKey();

27th Dec 2020, 8:56 PM
Mr Wolfy
Mr Wolfy - avatar
9 Answers
+ 4
Mr Wolfy, I'm using the following loop => foreach (string c in coffee.Keys.ToArray()){ //performing calculations }
27th Dec 2020, 10:17 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 9
foreach (string s in coffee.Keys.ToArray()) { coffee[s] -= (coffee[s]*discount)/100; Console.WriteLine(s+": "+coffee[s]); }
15th Mar 2021, 7:42 AM
Mehdi Kazemi
+ 5
Did the same of Wolfy with this inside coffee[s] -= (coffee[s]*discount)/100;
6th Jan 2021, 4:12 AM
Mauricio Klaus
Mauricio Klaus - avatar
+ 1
wow many interesting and complicated calculations but I just want to add mine; /////////////// foreach (string cf in coffee.Keys.ToArray()) { a = coffee[cf] * discount; a = a / 100; coffee[cf] -= a; Console.WriteLine("Value; {0}", coffee[cf]); }
11th Apr 2021, 6:54 PM
OmerPasa3328
OmerPasa3328 - avatar
+ 1
its simple and easy foreach(string s in coffee.Keys){ int a = coffee[s]- coffee[s]*discount/100; Console.WriteLine(s+":"+" "+a);
20th Jun 2021, 9:33 AM
Alam Sher Khan
Alam Sher Khan - avatar
0
O thanks i didnt know that i can use ToArray() in this way :o
28th Dec 2020, 5:45 PM
Mr Wolfy
Mr Wolfy - avatar
0
This is a nice challenge. I completed it similar to you guys and it passed example 1 and 2 but failed example 3 which is locked. So I can't see what is what. I pasted in the code at the top for fun. It's showing failing all 3 test cases despite having the right output. I think this one is bugged.
1st Aug 2021, 1:17 PM
John Gomeringer
John Gomeringer - avatar
0
foreach(KeyValuePair<string, int> entry in coffee) { Console.WriteLine(entry.Key +": "+(entry.Value-entry.Value*discount/100)); }
6th Mar 2022, 9:17 PM
Jackie
Jackie - avatar
0
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { int discount = Convert.ToInt32(Console.ReadLine()); Dictionary<string, double> coffee = new Dictionary<string, double>(); coffee.Add("Americano", 50); coffee.Add("Latte", 70); coffee.Add("Flat White", 60); coffee.Add("Espresso", 60); coffee.Add("Cappuccino", 80); coffee.Add("Mocha", 90); //your code goes here foreach (string s in coffee.Keys.ToArray()) { coffee[s] -= (coffee[s]*discount)/100; Console.WriteLine(s+": "+coffee[s]); } } } }
28th Jul 2023, 9:59 PM
Melta