diff --git a/csharp-fundamentals-maps.Main/Core.cs b/csharp-fundamentals-maps.Main/Core.cs index 8f4d84a..ead8e39 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,11 +48,9 @@ in the createPerson method public string getValue(string key) { - - - return string.Empty; - + var dict = createPerson(); + return dict.GetValueOrDefault(key); } //TODO: 2. Modify below method named hasKey that accepts two parameters: @@ -62,11 +60,12 @@ 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.ContainsKey(isitthere); + + } //TODO: 3. Modify method named getValueOrDefault that accepts two parameters: @@ -76,10 +75,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.TryGetValue(isitthere, out var value)) + { + return value; + } + else + { + return -1; + } } @@ -94,7 +99,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"); @@ -104,11 +109,17 @@ 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.TryGetValue(number, out string word)) + { + results.Add(word); + } + } - // // ...and above this comment return results; - } + } } } diff --git a/csharp-fundamentals-maps.Main/Extension.cs b/csharp-fundamentals-maps.Main/Extension.cs index 2deb96d..07a1f81 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,9 +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 planet in _planets) + { + result.Add(planet.Key, planet.Key.Length); + } - return result; } @@ -52,7 +54,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 +69,8 @@ public Dictionary OrderedPlanetsByDescending() public string FurthestFromTheSun() { - return string.Empty; + KeyValuePair result = OrderedPlanets().Last(); + return result.Key; } public string ClosestToTheSun() {