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
70 changes: 57 additions & 13 deletions exercise.main/Core.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,76 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main
{
public class Core
{


//TODO: complete the following method, keeping the signature the same
public bool winningPair(IEnumerable<Tuple<string, string>> hand, out Tuple<string, string> result)
{
result = new Tuple<string,string>(string.Empty, string.Empty);

result = new Tuple<string, string>(string.Empty, string.Empty);


if (hand == null || !hand.Any())
return false;


Dictionary<string, int> cardCounts = new Dictionary<string, int>();
List<Tuple<string, string>> winningPairs = new List<Tuple<string, string>>();

return result.Item1!=string.Empty ? true : false;
foreach (var card in hand)
{
string cardValue1 = card.Item1;
string cardValue2 = card.Item2;

// Check for a winning pair with cardValue1
if (cardCounts.ContainsKey(cardValue1))
{
cardCounts[cardValue1]++;
if (cardCounts[cardValue1] == 2)
{
winningPairs.Add(card);
}
}
else
{
cardCounts[cardValue1] = 1;
}

// Check for a winning pair with cardValue2
if (cardCounts.ContainsKey(cardValue2))
{
cardCounts[cardValue2]++;
if (cardCounts[cardValue2] == 2)
{
winningPairs.Add(card);
}
}
else
{
cardCounts[cardValue2] = 1;
}
}

if (winningPairs.Any())
{
// Find the pair with the highest value
result = winningPairs.OrderByDescending(pair => GetValueOfCard(pair.Item1)).First();
return true;
}

return false;
}

public int GetValueOfCard(string card)
{
return 0;
Dictionary<string, int> cardValues = new Dictionary<string, int>
{
{"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}
};

// Use the TryGetValue method to get the value of the card
return cardValues.TryGetValue(card, out int value) ? value : 0;
}
}
}
30 changes: 26 additions & 4 deletions exercise.main/Extension.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main
{
public class Extension
{
//TODO: complete the following method, keeping the signature the same
public bool winningThree(IEnumerable<Tuple<string, string, string>> hand, out Tuple<string, string, string> result)
public bool WinningThree(IEnumerable<Tuple<string, string, string>> hand, out Tuple<string, string, string> result)
{
result = new Tuple<string, string, string>(string.Empty, string.Empty, string.Empty);

// Dictionary to store the count of each card value
Dictionary<string, int> cardCounts = new Dictionary<string, int>();

// Iterate through the hand and count occurrences of each card value
foreach (var card in hand)
{
string cardValue = card.Item1;
if (cardCounts.ContainsKey(cardValue))
{
cardCounts[cardValue]++;
// Check for a winning triplet
if (cardCounts[cardValue] == 3)
{
result = card;
Console.WriteLine($"Found a winning triplet: {card.Item1} {card.Item2} {card.Item3}");
return true;
}
}
else
{
cardCounts[cardValue] = 1;
}
}

Console.WriteLine("No winning triplet found.");
return false;
}

}
}
2 changes: 0 additions & 2 deletions exercise.tests/CoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ public void Scenario1b()
[Test]
public void Scenario2()
{


Core core = new Core();

List<Tuple<string, string>> hand = new List<Tuple<string, string>>
Expand Down