From 5e236ae7141aa5e769ded2f9bd1470bdeb222ca0 Mon Sep 17 00:00:00 2001 From: Mona Eikli Andresen Date: Tue, 12 Aug 2025 13:44:20 +0200 Subject: [PATCH] Mona Eikli --- csharp-fundamentals-maps.Main/Core.cs | 32 ++++++++++++++-------- csharp-fundamentals-maps.Main/Extension.cs | 17 ++++++++---- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/csharp-fundamentals-maps.Main/Core.cs b/csharp-fundamentals-maps.Main/Core.cs index 8f4d84a..fd18631 100644 --- a/csharp-fundamentals-maps.Main/Core.cs +++ b/csharp-fundamentals-maps.Main/Core.cs @@ -48,11 +48,8 @@ in the createPerson method public string getValue(string key) { - - - return string.Empty; - - + var map = createPerson(); + return map.TryGetValue(key, out var value) ? value : string.Empty; } //TODO: 2. Modify below method named hasKey that accepts two parameters: @@ -62,9 +59,9 @@ public string getValue(string key) The method must return a boolean that represents whether the string provided exists as a key in the provided dictionary */ - public bool hasKey(Dictionary dictionary, string isitthere) + public bool hasKey(Dictionary dictionary, string isitthere) { - return false; + return dictionary.ContainsKey(isitthere); } @@ -76,13 +73,20 @@ public bool hasKey(Dictionary dictionary, string isitthere) The method must use the string provided to return the integer contained in the provided HashMap, or -1 if the string provided is not a key in the HashMap */ - public int getValueOrDefault(Dictionary dictionary, string isitthere) + public int getValueOrDefault(Dictionary dictionary, string isitthere) { - return 0; - + if (dictionary.ContainsKey(isitthere)) + { + return dictionary[isitthere]; + } + else + { + return -1; + } } + //TODO: 4. Complete the method below /* Example input & output: @@ -105,7 +109,13 @@ 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..1943361 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,10 @@ 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 (KeyValuePair planet in _planets) + { + result.Add(planet.Key, planet.Key.Length); + } return result; } @@ -52,9 +55,9 @@ 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 + //TODO: modify the OrderedPlanetsByDescending so dictionary is not doing an OrderBy but OrderByDescending @@ -66,8 +69,10 @@ public Dictionary OrderedPlanetsByDescending() public string FurthestFromTheSun() - { - return string.Empty; + { + + KeyValuePair result = OrderedPlanets().Last(); + return result.Key; } public string ClosestToTheSun() {