From 5c3e734a7fa71c459dfed54fe1d9108b2f4505c6 Mon Sep 17 00:00:00 2001 From: Aksel Saugestad Date: Wed, 8 Jan 2025 12:44:59 +0100 Subject: [PATCH] Finished exercise --- csharp-fundamentals-maps.Main/Core.cs | 30 ++++++++++++++++------ csharp-fundamentals-maps.Main/Extension.cs | 21 +++++++++------ csharp-fundamentals-maps.Main/Program.cs | 1 + 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/csharp-fundamentals-maps.Main/Core.cs b/csharp-fundamentals-maps.Main/Core.cs index 8f4d84a..565d026 100644 --- a/csharp-fundamentals-maps.Main/Core.cs +++ b/csharp-fundamentals-maps.Main/Core.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; @@ -48,10 +49,9 @@ in the createPerson method public string getValue(string key) { - - - return string.Empty; + Dictionary map = createPerson(); + return map[key]; } @@ -64,8 +64,7 @@ in the provided dictionary */ public bool hasKey(Dictionary dictionary, string isitthere) { - return false; - + return dictionary.ContainsKey(isitthere); } @@ -78,8 +77,13 @@ public bool hasKey(Dictionary dictionary, string isitthere) */ public int getValueOrDefault(Dictionary dictionary, string isitthere) { - return 0; - + if (dictionary.ContainsKey(isitthere)) + { + return dictionary[isitthere]; + }else + { + return -1; + } } @@ -105,7 +109,17 @@ 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]]); + } + else + { + break; + } + } // // ...and above this comment return results; diff --git a/csharp-fundamentals-maps.Main/Extension.cs b/csharp-fundamentals-maps.Main/Extension.cs index 2deb96d..4ef9ed0 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); @@ -27,7 +27,7 @@ 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(); @@ -36,7 +36,10 @@ 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. - + foreach (var planet in _planets) + { + result.Add(planet.Key, planet.Key.Length); + } return result; } @@ -50,9 +53,9 @@ 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); + public Dictionary OrderedPlanetsByDescending() + { + 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,9 @@ 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.Main/Program.cs b/csharp-fundamentals-maps.Main/Program.cs index 3751555..3ed0e5d 100644 --- a/csharp-fundamentals-maps.Main/Program.cs +++ b/csharp-fundamentals-maps.Main/Program.cs @@ -1,2 +1,3 @@ // See https://aka.ms/new-console-template for more information Console.WriteLine("Hello, World!"); +