diff --git a/csharp-fundamentals-maps.Main/Core.cs b/csharp-fundamentals-maps.Main/Core.cs index 8f4d84a..4e80488 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; + + var map = this.createPerson(); + return map[key]; // Should probably check if key exists in map } @@ -62,11 +62,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,9 +77,11 @@ 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]; + return -1; } @@ -94,7 +97,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 +108,14 @@ public List buildSecretPhrase(int[] numbers) map.Add(96, "nice"); // Write your code below this comment... - + for (int i = 0; i < numbers.Length; i++) + { + if (map.ContainsKey(numbers[i])) + results.Add(map[numbers[i]]); + } // // ...and above this comment return results; - } + } } } diff --git a/csharp-fundamentals-maps.Main/Extension.cs b/csharp-fundamentals-maps.Main/Extension.cs index 2deb96d..59351ce 100644 --- a/csharp-fundamentals-maps.Main/Extension.cs +++ b/csharp-fundamentals-maps.Main/Extension.cs @@ -7,7 +7,7 @@ namespace csharp_fundamentals_maps.Main { - public class Extension + public class Extension { private Dictionary _planets; @@ -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); @@ -26,17 +26,20 @@ public Extension() } //TODO Pluto is unfortunately no longer a planet so please comment out the add line! - - public Dictionary LettersInName() + + 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 p in this._planets.Keys) + { + result[p] = p.Length; + } return result; } @@ -46,13 +49,14 @@ public Dictionary LettersInName() - public Dictionary OrderedPlanets() - { - return _planets.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value); + 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.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 @@ -63,11 +67,12 @@ public Dictionary OrderedPlanetsByDescending() // 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() { @@ -76,7 +81,7 @@ public string ClosestToTheSun() return result.Key; } - public Dictionary Planets { get { return _planets; } } + public Dictionary Planets { get { return _planets; } } }