From 06a481f67966401c2e7558661ec57429ded36d86 Mon Sep 17 00:00:00 2001 From: Jonatan Berg Romundgard Date: Wed, 13 Aug 2025 15:54:30 +0200 Subject: [PATCH] Passed all tests --- csharp-fundamentals-maps.Main/Core.cs | 21 +++++++++++-------- csharp-fundamentals-maps.Main/Extension.cs | 12 +++++++---- csharp-fundamentals-maps.Test/CoreTests.cs | 2 ++ .../ExtensionTests.cs | 3 +-- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/csharp-fundamentals-maps.Main/Core.cs b/csharp-fundamentals-maps.Main/Core.cs index 8f4d84a..4a434fd 100644 --- a/csharp-fundamentals-maps.Main/Core.cs +++ b/csharp-fundamentals-maps.Main/Core.cs @@ -48,11 +48,8 @@ in the createPerson method public string getValue(string key) { - - - return string.Empty; - - + Dictionary map = createPerson(); + return map[key]; } //TODO: 2. Modify below method named hasKey that accepts two parameters: @@ -64,8 +61,7 @@ in the provided dictionary */ public bool hasKey(Dictionary dictionary, string isitthere) { - return false; - + return dictionary.ContainsKey(isitthere); } @@ -78,7 +74,11 @@ public bool hasKey(Dictionary dictionary, string isitthere) */ public int getValueOrDefault(Dictionary dictionary, string isitthere) { - return 0; + if (dictionary.ContainsKey(isitthere)) + { + return dictionary[isitthere]; + } + return -1; } @@ -105,7 +105,10 @@ public List buildSecretPhrase(int[] numbers) map.Add(96, "nice"); // Write your code below this comment... - + foreach (int i in numbers) + { + results.Add(map[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..6573901 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,7 +35,10 @@ 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[planet.Key] = planet.Key.Length; + } return result; @@ -52,7 +55,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 +70,8 @@ public Dictionary OrderedPlanetsByDescending() public string FurthestFromTheSun() { - return string.Empty; + KeyValuePair result = OrderedPlanets().Last(); + return result.Key; } public string ClosestToTheSun() { diff --git a/csharp-fundamentals-maps.Test/CoreTests.cs b/csharp-fundamentals-maps.Test/CoreTests.cs index 591851a..3f8efd4 100644 --- a/csharp-fundamentals-maps.Test/CoreTests.cs +++ b/csharp-fundamentals-maps.Test/CoreTests.cs @@ -15,6 +15,8 @@ public CoreTests() [Test] public void getValueTest() { + //Core core = new Core(); + //core.createPerson(); Assert.AreEqual("Nigel", this.exercise.getValue("firstName")); Assert.AreEqual("Sibbert", this.exercise.getValue("lastName")); Assert.AreEqual("Software Developer", this.exercise.getValue("occupation")); diff --git a/csharp-fundamentals-maps.Test/ExtensionTests.cs b/csharp-fundamentals-maps.Test/ExtensionTests.cs index 14d899e..57203ef 100644 --- a/csharp-fundamentals-maps.Test/ExtensionTests.cs +++ b/csharp-fundamentals-maps.Test/ExtensionTests.cs @@ -22,8 +22,7 @@ public ExtensionTests() [Test] public void PlutoNotAPlanet() { - - Assert.IsFalse(ext.Planets.ContainsKey("Pluto")); + Assert.That(ext.Planets.ContainsKey("Pluto"), Is.False); } [Test]