diff --git a/exercise.main/Core.cs b/exercise.main/Core.cs index bf5077d..808dad9 100644 --- a/exercise.main/Core.cs +++ b/exercise.main/Core.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; +using System.Linq.Expressions; using System.Reflection; using System.Runtime.InteropServices; using System.Text; @@ -10,23 +12,69 @@ namespace exercise.main { public class Core { - + // Note: // IEnumerable is a class that the List inherits (ie List has all the functionality of IEnumerable) // IEnumerable allows you to use a foreach loop on the variable hands (which is a List of tuples) - + //TODO: complete the winningPair method, without chaning the method signature - public bool winningPair(IEnumerable> hands, out Tuple result) - { - result = new Tuple(string.Empty, string.Empty); + public bool winningPair(IEnumerable> hands, out Tuple result) { + + + + result = new Tuple(string.Empty, string.Empty); + // your code here... - return result.Item1!=string.Empty ? true : false; + List> winning_Hands = + hands.Where(hand => hand.Item1 == hand.Item2).ToList(); + + int current_sum = 0; + int hand_sum = 0; + + foreach (var item in winning_Hands) + { + hand_sum = GetValueOfCard(item.Item1) + GetValueOfCard(item.Item2); + if (hand_sum > current_sum) + { + current_sum = hand_sum; + result = item; + } + } + + return result.Item1 != string.Empty? true : false; } + public int GetValueOfCard(string card) { - return 0; + var card_Values = new Dictionary() + { + { "2", 2}, + { "3", 3}, + { "4", 4}, + { "5", 5}, + { "6", 6}, + { "7", 7}, + { "8", 8}, + { "9", 9}, + { "10", 10}, + { "J", 11}, + { "Q", 12}, + { "K", 13}, + { "A", 14} + }; + + if (card_Values.TryGetValue(card, out int cardValue)) + { + return cardValue; + + } + + return -1; + } } } + + diff --git a/exercise.main/Extension.cs b/exercise.main/Extension.cs index b33ba1f..cb7509b 100644 --- a/exercise.main/Extension.cs +++ b/exercise.main/Extension.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection.Metadata; using System.Text; using System.Threading.Tasks; @@ -11,10 +12,57 @@ public class Extension //TODO: complete the following method, keeping the signature the same public bool winningThree(IEnumerable> hand, out Tuple result) { + List> winning_Hands = + hand.Where(hand => hand.Item1 == hand.Item2 && hand.Item1 == hand.Item3).ToList(); + + int current_sum = 0; + int hand_sum = 0; + + foreach (var item in winning_Hands) + { + hand_sum = GetValueOfCard(item.Item1) + GetValueOfCard(item.Item2) + GetValueOfCard(item.Item3); + if (hand_sum > current_sum) + { + current_sum = hand_sum; + result = item; + } + } + + result = new Tuple(string.Empty, string.Empty, string.Empty); return false; } + + + public int GetValueOfCard(string card) + { + var card_Values = new Dictionary() + { + { "2", 2}, + { "3", 3}, + { "4", 4}, + { "5", 5}, + { "6", 6}, + { "7", 7}, + { "8", 8}, + { "9", 9}, + { "10", 10}, + { "J", 11}, + { "Q", 12}, + { "K", 13}, + { "A", 14} + }; + + if (card_Values.TryGetValue(card, out int cardValue)) + { + return cardValue; + + } + + return -1; + + } } -} +} \ No newline at end of file