From 9e6b62b9e63f90b9880b07a306abfd50affc9a37 Mon Sep 17 00:00:00 2001 From: Vegard Stigen Date: Tue, 12 Aug 2025 11:31:52 +0200 Subject: [PATCH] completed, all tests pass --- csharp-fundamentals-maps.Main/Core.cs | 24 +++++++++++++++++----- csharp-fundamentals-maps.Main/Extension.cs | 12 +++++++---- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/csharp-fundamentals-maps.Main/Core.cs b/csharp-fundamentals-maps.Main/Core.cs index 8f4d84a..cb093cd 100644 --- a/csharp-fundamentals-maps.Main/Core.cs +++ b/csharp-fundamentals-maps.Main/Core.cs @@ -49,8 +49,8 @@ in the createPerson method public string getValue(string key) { - - return string.Empty; + Dictionary map = createPerson(); + return map[key]; } @@ -64,9 +64,13 @@ in the provided dictionary */ public bool hasKey(Dictionary dictionary, string isitthere) { + if (dictionary.ContainsKey(isitthere)) + { + return true; + } return false; - - } + + } //TODO: 3. Modify method named getValueOrDefault that accepts two parameters: @@ -78,6 +82,10 @@ public bool hasKey(Dictionary dictionary, string isitthere) */ public int getValueOrDefault(Dictionary dictionary, string isitthere) { + if (dictionary.ContainsKey(isitthere)) + { + return dictionary[isitthere]; + } return 0; } @@ -104,7 +112,13 @@ public List buildSecretPhrase(int[] numbers) map.Add(7, "muse"); 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 diff --git a/csharp-fundamentals-maps.Main/Extension.cs b/csharp-fundamentals-maps.Main/Extension.cs index 2deb96d..e72bec0 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,8 +35,11 @@ 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 (var item in _planets) + { + result.Add(item.Key, item.Key.ToString().Length); + } - return result; } @@ -52,7 +55,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 +70,8 @@ public Dictionary OrderedPlanetsByDescending() public string FurthestFromTheSun() { - return string.Empty; + KeyValuePair result = OrderedPlanetsByDescending().First(); + return result.Key; } public string ClosestToTheSun() {