diff --git a/csharp-fundamentals-maps.Main/Core.cs b/csharp-fundamentals-maps.Main/Core.cs index 8f4d84a..18b936a 100644 --- a/csharp-fundamentals-maps.Main/Core.cs +++ b/csharp-fundamentals-maps.Main/Core.cs @@ -46,11 +46,12 @@ public Dictionary createPerson() in the createPerson method */ + public string getValue(string key) { - - - return string.Empty; + + + return this.createPerson()[key]; } @@ -64,9 +65,9 @@ in the provided dictionary */ public bool hasKey(Dictionary dictionary, string isitthere) { - return false; - - } + return dictionary.ContainsKey(isitthere); + + } //TODO: 3. Modify method named getValueOrDefault that accepts two parameters: @@ -76,9 +77,14 @@ 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; } @@ -104,7 +110,9 @@ public List buildSecretPhrase(int[] numbers) map.Add(7, "muse"); map.Add(96, "nice"); // Write your code below this comment... + map.Add(19, "nice"); + numbers.ToList().ForEach(n => results.Add(map[n])); // // ...and above this comment diff --git a/csharp-fundamentals-maps.Main/Extension.cs b/csharp-fundamentals-maps.Main/Extension.cs index 2deb96d..07861fc 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,7 +36,7 @@ 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. - + this._planets.ToList().ForEach(x => result.Add(x.Key, x.Key.Count())); return result; } @@ -51,8 +51,11 @@ 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); + { + Dictionary result = _planets.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value); + // return _planets.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value); + return result; + } //TODO: modify the OrderedPlanetsByDescending so it is not dictionary is not doing an OrderBy but OrderByDescending @@ -67,7 +70,8 @@ public Dictionary OrderedPlanetsByDescending() public string FurthestFromTheSun() { - return string.Empty; + KeyValuePair result = OrderedPlanets().Last(); + return result.Key; } public string ClosestToTheSun() {