From df19496aedfba901791c95a918c9f06aa68b7d1e Mon Sep 17 00:00:00 2001 From: Hans Eivind Johnsen Skinstad Date: Wed, 8 Jan 2025 11:40:09 +0100 Subject: [PATCH] All tests pass. Exercise finished! --- csharp-fundamentals-maps.Main/Core.cs | 13 +++++++------ csharp-fundamentals-maps.Main/Extension.cs | 11 +++++++---- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/csharp-fundamentals-maps.Main/Core.cs b/csharp-fundamentals-maps.Main/Core.cs index 8f4d84a..2b52934 100644 --- a/csharp-fundamentals-maps.Main/Core.cs +++ b/csharp-fundamentals-maps.Main/Core.cs @@ -48,9 +48,9 @@ in the createPerson method public string getValue(string key) { - + - return string.Empty; + return createPerson()[key]; } @@ -64,7 +64,7 @@ in the provided dictionary */ public bool hasKey(Dictionary dictionary, string isitthere) { - return false; + return dictionary.ContainsKey(isitthere); } @@ -78,7 +78,7 @@ public bool hasKey(Dictionary dictionary, string isitthere) */ public int getValueOrDefault(Dictionary dictionary, string isitthere) { - return 0; + return dictionary.GetValueOrDefault(isitthere); } @@ -104,8 +104,9 @@ public List buildSecretPhrase(int[] numbers) map.Add(7, "muse"); map.Add(96, "nice"); // Write your code below this comment... - - + foreach(var item in numbers) + if(map.ContainsKey(item)) + results.Add(map[item]); // // ...and above this comment return results; diff --git a/csharp-fundamentals-maps.Main/Extension.cs b/csharp-fundamentals-maps.Main/Extension.cs index 2deb96d..6cc2395 100644 --- a/csharp-fundamentals-maps.Main/Extension.cs +++ b/csharp-fundamentals-maps.Main/Extension.cs @@ -16,7 +16,7 @@ public Extension() _planets = new Dictionary(); _planets.Add("Jupiter", 5.2f); _planets.Add("Uranus", 19.2f); - _planets.Add("Pluto", 39f); + //_planets.Add("Pluto", 39f); _planets.Add("Mercury", 0.39f); _planets.Add("Saturn", 9.54f); _planets.Add("Earth", 1f); @@ -36,7 +36,8 @@ public Dictionary LettersInName() // the planet name and the number of letters in its name // iterate the _planets using a foreach object to load the result dictionary. - + foreach (var planet in _planets) + result.Add(planet.Key, planet.Key.Length); return result; } @@ -52,7 +53,7 @@ public Dictionary OrderedPlanets() } public Dictionary OrderedPlanetsByDescending() { - return _planets.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value); + return _planets.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value); } //TODO: modify the OrderedPlanetsByDescending so it is not dictionary is not doing an OrderBy but OrderByDescending @@ -67,7 +68,9 @@ public Dictionary OrderedPlanetsByDescending() public string FurthestFromTheSun() { - return string.Empty; + KeyValuePair result = OrderedPlanetsByDescending().First(); + + return result.Key; } public string ClosestToTheSun() {