Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 30 additions & 27 deletions csharp-fundamentals-maps.Main/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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, V>
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<string, string>
*/
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, V>
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<string, string>
*/

//TODO: Spend some time understanding the method below
/*
Expand Down Expand Up @@ -48,9 +48,9 @@ in the createPerson method

public string getValue(string key)
{
return string.Empty;


return createPerson()[key];


}
Expand All @@ -62,11 +62,11 @@ 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<string,string> dictionary, string isitthere)
{
return false;
}
public bool hasKey(Dictionary<string, string> dictionary, string isitthere)
{
return dictionary.GetValueOrDefault(isitthere) != null;

}


//TODO: 3. Modify method named getValueOrDefault that accepts two parameters:
Expand All @@ -76,9 +76,9 @@ public bool hasKey(Dictionary<string,string> 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<string,int> dictionary, string isitthere)
public int getValueOrDefault(Dictionary<string, int> dictionary, string isitthere)
{
return 0;
return dictionary.GetValueOrDefault(isitthere, -1);

}

Expand All @@ -94,7 +94,7 @@ input output
public List<string> buildSecretPhrase(int[] numbers)
{
List<string> results = new List<string>();

// Do not modify the map
Dictionary<int, string> map = new Dictionary<int, string>();
map.Add(23, "chicken");
Expand All @@ -105,10 +105,13 @@ public List<string> buildSecretPhrase(int[] numbers)
map.Add(96, "nice");
// Write your code below this comment...


foreach (int n in numbers)
{
results.Add(map.GetValueOrDefault(n, "unkown"));
}

// // ...and above this comment
// ...and above this comment
return results;
}
}
}
}
13 changes: 4 additions & 9 deletions csharp-fundamentals-maps.Main/Extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public Extension()
_planets = new Dictionary<string, float>();
_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);
Expand All @@ -36,23 +36,18 @@ public Dictionary<string,int> LettersInName()
// the planet name and the number of letters in its name
// iterate the _planets using a foreach object to load the result dictionary.


_planets.Keys.ToList().ForEach(k => result[k] = k.Length);

return result;
}






public Dictionary<string,float> OrderedPlanets()
{
return _planets.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
}
public Dictionary<string, float> 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

Expand All @@ -67,7 +62,7 @@ public Dictionary<string, float> OrderedPlanetsByDescending()

public string FurthestFromTheSun()
{
return string.Empty;
return OrderedPlanetsByDescending().Keys.First();
}
public string ClosestToTheSun()
{
Expand Down