From 993d313507d93885f2dbd2d462100ab4f4196528 Mon Sep 17 00:00:00 2001 From: Patrick Schuur Date: Tue, 19 Aug 2025 13:43:11 +0200 Subject: [PATCH] Finished up until extensions --- .gitignore | 3 ++ csharp-fundamentals-maps.Main/Core.cs | 60 ++++++++++------------ csharp-fundamentals-maps.Main/Extension.cs | 44 ++++------------ 3 files changed, 40 insertions(+), 67 deletions(-) diff --git a/.gitignore b/.gitignore index 9491a2f..81ec018 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,9 @@ *.userosscache *.sln.docstates +# Jetbrains Project Files +.idea/ + # User-specific files (MonoDevelop/Xamarin Studio) *.userprefs diff --git a/csharp-fundamentals-maps.Main/Core.cs b/csharp-fundamentals-maps.Main/Core.cs index 8f4d84a..177015b 100644 --- a/csharp-fundamentals-maps.Main/Core.cs +++ b/csharp-fundamentals-maps.Main/Core.cs @@ -6,19 +6,18 @@ 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 /* @@ -26,7 +25,7 @@ If we wanted to map a persons details (their first name, last name, occupation e - It adds some keys with values - Under the hood, the Dictionary would look like this: { "firstName": "Nathan", "lastName": King, etc. } - It returns the Dictionary - + */ public Dictionary createPerson() { @@ -48,25 +47,20 @@ in the createPerson method public string getValue(string key) { - - - return string.Empty; - - + return createPerson()[key]; } //TODO: 2. Modify below method named hasKey that accepts two parameters: /* - - A Dictionary of string, string key value pairs + - A Dictionary of string, string key value pairs - string 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.ContainsKey(isitthere); + } //TODO: 3. Modify method named getValueOrDefault that accepts two parameters: @@ -76,10 +70,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 +87,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 +98,13 @@ public List buildSecretPhrase(int[] numbers) map.Add(96, "nice"); // Write your code below this comment... - + foreach (int number in numbers) + { + results.Add(map[number]); + } // // ...and above this comment return results; - } + } } -} +} \ No newline at end of file diff --git a/csharp-fundamentals-maps.Main/Extension.cs b/csharp-fundamentals-maps.Main/Extension.cs index 2deb96d..e5ac69b 100644 --- a/csharp-fundamentals-maps.Main/Extension.cs +++ b/csharp-fundamentals-maps.Main/Extension.cs @@ -1,11 +1,4 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace csharp_fundamentals_maps.Main +namespace csharp_fundamentals_maps.Main { public class Extension { @@ -16,7 +9,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); @@ -24,50 +17,33 @@ public Extension() _planets.Add("Venus", 0.72f); _planets.Add("Neptune", 30.06f); } - //TODO Pluto is unfortunately no longer a planet so please comment out the add line! - public Dictionary LettersInName() { Dictionary result = new Dictionary(); - //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 planet in _planets) + { + result.Add(planet.Key, planet.Key.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 - - - - // - //TODO using the OrderedPlanets method get the - // furthest from the sun. - // Update the method to return the correct KeyValuePair's Key (the string)! - // Use the ClosestToTheSun as a guide - public string FurthestFromTheSun() { - return string.Empty; + KeyValuePair result = OrderedPlanets().Last(); + return result.Key; } public string ClosestToTheSun() { @@ -77,7 +53,5 @@ public string ClosestToTheSun() } public Dictionary Planets { get { return _planets; } } - - } }