From ac05cfe8a2ddba13d0375d09d7be9a27fc5e331b Mon Sep 17 00:00:00 2001 From: Chris Sivert Sylte Date: Wed, 27 Aug 2025 11:30:23 +0200 Subject: [PATCH] didnt add all --- csharp-fundamentals-maps.Main/Core.cs | 38 +++++++++++++++------- csharp-fundamentals-maps.Main/Extension.cs | 19 ++++++++--- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/csharp-fundamentals-maps.Main/Core.cs b/csharp-fundamentals-maps.Main/Core.cs index 8f4d84a..fa82ff5 100644 --- a/csharp-fundamentals-maps.Main/Core.cs +++ b/csharp-fundamentals-maps.Main/Core.cs @@ -48,11 +48,12 @@ in the createPerson method public string getValue(string key) { - - - return string.Empty; - - + var person = createPerson(); + if (person.ContainsKey(key)) + { + return person[key]; + } + return null; } //TODO: 2. Modify below method named hasKey that accepts two parameters: @@ -64,7 +65,7 @@ in the provided dictionary */ public bool hasKey(Dictionary dictionary, string isitthere) { - return false; + return dictionary.ContainsKey(isitthere); } @@ -76,9 +77,16 @@ 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; + } } @@ -94,6 +102,7 @@ input output public List buildSecretPhrase(int[] numbers) { List results = new List(); + string value; // Do not modify the map Dictionary map = new Dictionary(); @@ -103,12 +112,17 @@ public List buildSecretPhrase(int[] numbers) map.Add(6712, "bass"); map.Add(7, "muse"); map.Add(96, "nice"); - // Write your code below this comment... - + foreach (var number in numbers) + { + if (map.ContainsKey(number)) + { + value = map[number]; + results.Add(value); + } - // // ...and above this comment - return results; + } + return results; } } } diff --git a/csharp-fundamentals-maps.Main/Extension.cs b/csharp-fundamentals-maps.Main/Extension.cs index 2deb96d..b317769 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); @@ -31,13 +31,19 @@ public Dictionary LettersInName() { Dictionary result = new Dictionary(); + string currentKey; + + foreach (KeyValuePair kvp in _planets) + { + currentKey = kvp.Key; + int nameLength = currentKey.Length; + result.Add(currentKey, nameLength); + } //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. - - return result; } @@ -52,7 +58,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,8 +73,11 @@ public Dictionary OrderedPlanetsByDescending() public string FurthestFromTheSun() { - return string.Empty; + KeyValuePair result = OrderedPlanets().Last(); + + return result.Key; } + public string ClosestToTheSun() { KeyValuePair result = OrderedPlanets().First();