From 7442a1dcefc2841fe6dcb5905d87ee8f32a24337 Mon Sep 17 00:00:00 2001 From: Kristoffer Blucher Date: Wed, 8 Jan 2025 13:16:32 +0100 Subject: [PATCH] Finished exercise --- csharp-fundamentals-maps.Main/Core.cs | 30 +++++++++++++++++----- csharp-fundamentals-maps.Main/Extension.cs | 13 +++++++--- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/csharp-fundamentals-maps.Main/Core.cs b/csharp-fundamentals-maps.Main/Core.cs index 8f4d84a..0299f86 100644 --- a/csharp-fundamentals-maps.Main/Core.cs +++ b/csharp-fundamentals-maps.Main/Core.cs @@ -48,11 +48,9 @@ in the createPerson method public string getValue(string key) { - - - return string.Empty; - + Dictionary personMap = createPerson(); + return personMap[key]; } //TODO: 2. Modify below method named hasKey that accepts two parameters: @@ -64,7 +62,9 @@ in the provided dictionary */ public bool hasKey(Dictionary dictionary, string isitthere) { - return false; + + + return dictionary.ContainsKey(isitthere); } @@ -78,7 +78,12 @@ public bool hasKey(Dictionary dictionary, string isitthere) */ public int getValueOrDefault(Dictionary dictionary, string isitthere) { - return 0; + if (dictionary.ContainsKey(isitthere) ) + + return dictionary[isitthere]; + + + return -1; } @@ -105,8 +110,19 @@ public List buildSecretPhrase(int[] numbers) map.Add(96, "nice"); // Write your code below this comment... - + foreach (int number in numbers) + + { + + if (map.ContainsKey(number)) + + { + + results.Add(map[number]); + } + } + // // ...and above this comment return results; } diff --git a/csharp-fundamentals-maps.Main/Extension.cs b/csharp-fundamentals-maps.Main/Extension.cs index 2deb96d..dd9c3dc 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); @@ -35,7 +35,12 @@ public Dictionary LettersInName() //TODO Complete this method to return an Dictionary of which contains // the planet name and the number of letters in its name // iterate the _planets using a foreach object to load the result dictionary. + foreach (string planet in _planets.Keys) + { + result.Add(planet, planet.Length); + + } return result; @@ -52,7 +57,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 +72,9 @@ public Dictionary OrderedPlanetsByDescending() public string FurthestFromTheSun() { - return string.Empty; + KeyValuePair result = OrderedPlanetsByDescending().First(); + + return result.Key; } public string ClosestToTheSun() {