From 4868f7d221ab3056026f0c1ffe4784e235f8e41a Mon Sep 17 00:00:00 2001 From: Reduan Azouaghe Date: Tue, 12 Aug 2025 12:41:27 +0200 Subject: [PATCH] Finished core and extention. --- csharp-fundamentals-maps.Main/Core.cs | 57 ++++++++++++---------- csharp-fundamentals-maps.Main/Extension.cs | 13 ++--- 2 files changed, 34 insertions(+), 36 deletions(-) diff --git a/csharp-fundamentals-maps.Main/Core.cs b/csharp-fundamentals-maps.Main/Core.cs index 8f4d84a..687c997 100644 --- a/csharp-fundamentals-maps.Main/Core.cs +++ b/csharp-fundamentals-maps.Main/Core.cs @@ -6,19 +6,19 @@ namespace csharp_fundamentals_maps.Main { - public class Core - { - - /* - The final fundamental building block of C# is a Dictionary. There is still much to learn about the language, - but this component will allow you to start building lots of more complex pieces of software. - - Dictionary - K is where you'd put the data type of the key for an item, V is the data type of the value. - If we wanted to map a persons details (their first name, last name, occupation etc.), we could use - a Dictionary using a String key and a String value like so: - Dictionary - */ + public class Core + { + + /* + The final fundamental building block of C# is a Dictionary. There is still much to learn about the language, + but this component will allow you to start building lots of more complex pieces of software. + + Dictionary + K is where you'd put the data type of the key for an item, V is the data type of the value. + If we wanted to map a persons details (their first name, last name, occupation etc.), we could use + a Dictionary using a String key and a String value like so: + Dictionary + */ //TODO: Spend some time understanding the method below /* @@ -48,9 +48,9 @@ in the createPerson method public string getValue(string key) { - - - return string.Empty; + + + return createPerson()[key]; } @@ -62,11 +62,11 @@ 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) - { - return false; - - } + public bool hasKey(Dictionary dictionary, string isitthere) + { + return dictionary.GetValueOrDefault(isitthere) != null; + + } //TODO: 3. Modify method named getValueOrDefault that accepts two parameters: @@ -76,9 +76,9 @@ 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; + return dictionary.GetValueOrDefault(isitthere, -1); } @@ -94,7 +94,7 @@ input output public List buildSecretPhrase(int[] numbers) { List results = new List(); - + // Do not modify the map Dictionary map = new Dictionary(); map.Add(23, "chicken"); @@ -105,10 +105,13 @@ public List buildSecretPhrase(int[] numbers) map.Add(96, "nice"); // Write your code below this comment... - + foreach (int n in numbers) + { + results.Add(map.GetValueOrDefault(n, "unkown")); + } - // // ...and above this comment + // ...and above this comment return results; - } + } } } diff --git a/csharp-fundamentals-maps.Main/Extension.cs b/csharp-fundamentals-maps.Main/Extension.cs index 2deb96d..0f9a390 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,23 +36,18 @@ 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. - + _planets.Keys.ToList().ForEach(k => result[k] = k.Length); return result; } - - - - - public Dictionary OrderedPlanets() { return _planets.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value); } 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 +62,7 @@ public Dictionary OrderedPlanetsByDescending() public string FurthestFromTheSun() { - return string.Empty; + return OrderedPlanetsByDescending().Keys.First(); } public string ClosestToTheSun() {