From 2d8c57a07e3e1847aba049793a28b37beef03fd8 Mon Sep 17 00:00:00 2001 From: Erik Bergvall Date: Wed, 8 Jan 2025 13:03:02 +0100 Subject: [PATCH 1/2] Completed base exercise --- csharp-fundamentals-maps.Main/Core.cs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/csharp-fundamentals-maps.Main/Core.cs b/csharp-fundamentals-maps.Main/Core.cs index 8f4d84a..79d252a 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; + + var dict = createPerson(); + return dict[key]; } @@ -64,7 +64,10 @@ in the provided dictionary */ public bool hasKey(Dictionary dictionary, string isitthere) { - return false; + if(dictionary.ContainsKey(isitthere)) + return true; + else + return false; } @@ -78,7 +81,10 @@ public bool hasKey(Dictionary dictionary, string isitthere) */ public int getValueOrDefault(Dictionary dictionary, string isitthere) { - return 0; + if (dictionary.ContainsKey(isitthere)) + return dictionary[isitthere]; + else + return -1; } @@ -104,8 +110,12 @@ public List buildSecretPhrase(int[] numbers) map.Add(7, "muse"); map.Add(96, "nice"); // Write your code below this comment... + foreach (var num in numbers) + { + if( map.ContainsKey(num)) + results.Add(map[num]); + } - // // ...and above this comment return results; From 7bc1fd48bc22c0c29760cccac4d3d33b9fa97657 Mon Sep 17 00:00:00 2001 From: Erik Bergvall Date: Wed, 15 Jan 2025 10:48:26 +0100 Subject: [PATCH 2/2] Completed the extension exercise aswell --- csharp-fundamentals-maps.Main/Extension.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/csharp-fundamentals-maps.Main/Extension.cs b/csharp-fundamentals-maps.Main/Extension.cs index 2deb96d..3f44542 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,12 @@ 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 item in _planets) + { + result.Add(item.Key, item.Key.Count()); + } + + 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 = OrderedPlanets().Last(); + + return result.Key; } public string ClosestToTheSun() {