From 74540c63e8ed3268289f0a654d5ee7cfe4fcbc16 Mon Sep 17 00:00:00 2001 From: Johan Reitan Date: Wed, 13 Aug 2025 14:12:59 +0200 Subject: [PATCH] all green --- csharp-fundamentals-maps.Main/Core.cs | 18 +++++++----------- csharp-fundamentals-maps.Main/Extension.cs | 12 +++++++++--- csharp-fundamentals-maps.Test/CoreTests.cs | 6 ++++++ 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/csharp-fundamentals-maps.Main/Core.cs b/csharp-fundamentals-maps.Main/Core.cs index 8f4d84a..fcc7a2f 100644 --- a/csharp-fundamentals-maps.Main/Core.cs +++ b/csharp-fundamentals-maps.Main/Core.cs @@ -48,11 +48,7 @@ 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: @@ -64,7 +60,7 @@ in the provided dictionary */ public bool hasKey(Dictionary dictionary, string isitthere) { - return false; + return dictionary.ContainsKey(isitthere); } @@ -78,8 +74,7 @@ public bool hasKey(Dictionary dictionary, string isitthere) */ public int getValueOrDefault(Dictionary dictionary, string isitthere) { - return 0; - + return dictionary[isitthere]; } @@ -104,9 +99,10 @@ public List buildSecretPhrase(int[] numbers) map.Add(7, "muse"); map.Add(96, "nice"); // Write your code below this comment... - - - + foreach (int number in numbers) + { + results.Add(map.GetValueOrDefault(number)); + } // // ...and above this comment return results; } diff --git a/csharp-fundamentals-maps.Main/Extension.cs b/csharp-fundamentals-maps.Main/Extension.cs index 2deb96d..9f8f993 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); @@ -32,6 +32,11 @@ public Dictionary LettersInName() Dictionary result = new Dictionary(); + foreach(var p in _planets) + { + result.Add(p.Key, p.Key.Count()); + } + //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. @@ -52,7 +57,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 +72,8 @@ public Dictionary OrderedPlanetsByDescending() public string FurthestFromTheSun() { - return string.Empty; + + return OrderedPlanetsByDescending().First().Key; } public string ClosestToTheSun() { diff --git a/csharp-fundamentals-maps.Test/CoreTests.cs b/csharp-fundamentals-maps.Test/CoreTests.cs index 591851a..e621f74 100644 --- a/csharp-fundamentals-maps.Test/CoreTests.cs +++ b/csharp-fundamentals-maps.Test/CoreTests.cs @@ -15,6 +15,12 @@ public CoreTests() [Test] public void getValueTest() { + Dictionary map = new Dictionary(); + + map.Add("firstName", "Nigel"); + map.Add("lastName", "Sibbert"); + map.Add("currentTown", "Bournemouth"); + Assert.AreEqual("Nigel", this.exercise.getValue("firstName")); Assert.AreEqual("Sibbert", this.exercise.getValue("lastName")); Assert.AreEqual("Software Developer", this.exercise.getValue("occupation"));