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
64 changes: 53 additions & 11 deletions exercise.main/Core.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,74 @@
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);


Dictionary<string, int> cardValues = new Dictionary<string, int>();
bool hasPair = false;


foreach (var pair in hand)
{
string card1 = pair.Item1;
string card2 = pair.Item2;

return result.Item1!=string.Empty ? true : false;
int value1 = GetValueOfCard(card1);
int value2 = GetValueOfCard(card2);

if (value1 == value2)
{
hasPair = true;

if (!cardValues.ContainsKey(card1))
{
cardValues[card1] = value1;
}

if (!cardValues.ContainsKey(card2))
{
cardValues[card2] = value2;
}
}
}

if (hasPair)
{
var highestPair = cardValues.OrderByDescending(x => x.Value).FirstOrDefault();

result = new Tuple<string, string>(highestPair.Key, highestPair.Key);
return true;
}

return false;
}

public int GetValueOfCard(string card)
{
return 0;
switch (card)
{
case "J":
return 11;
case "Q":
return 12;
case "K":
return 13;
case "A":
return 14;
default:
if (int.TryParse(card, out int value))
{
return value;
}
return 0;
}
}
}
}
65 changes: 63 additions & 2 deletions exercise.main/Extension.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main
{
Expand All @@ -13,8 +11,71 @@ public bool winningThree(IEnumerable<Tuple<string, string, string>> hand, out Tu
{
result = new Tuple<string, string, string>(string.Empty, string.Empty, string.Empty);

Dictionary<string, int> cardValues = new Dictionary<string, int>();
bool hasTriplet = false;

foreach (var triplet in hand)
{
string card1 = triplet.Item1;
string card2 = triplet.Item2;
string card3 = triplet.Item3;

int value1 = GetValueOfCard(card1);
int value2 = GetValueOfCard(card2);
int value3 = GetValueOfCard(card3);

if (value1 == value2 && value2 == value3)
{
hasTriplet = true;

if (!cardValues.ContainsKey(card1))
{
cardValues[card1] = value1;
}

// Add the other cards to the dictionary if not present
if (!cardValues.ContainsKey(card2))
{
cardValues[card2] = value2;
}

if (!cardValues.ContainsKey(card3))
{
cardValues[card3] = value3;
}
}
}

if (hasTriplet)
{
var highestTriplet = cardValues.OrderByDescending(x => x.Value).FirstOrDefault();

result = new Tuple<string, string, string>(highestTriplet.Key, highestTriplet.Key, highestTriplet.Key);
return true;
}

return false;
}

public int GetValueOfCard(string card)
{
switch (card)
{
case "J":
return 11;
case "Q":
return 12;
case "K":
return 13;
case "A":
return 14;
default:
if (int.TryParse(card, out int value))
{
return value;
}
return 0;
}
}
}
}
76 changes: 76 additions & 0 deletions exercise.tests/ExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using exercise.main;
using NUnit.Framework;
using System;
using System.Collections.Generic;

namespace exercise.tests
{
[TestFixture]
public class ExtensionTests
{
[Test]
public void WinningThree_Scenario1()
{
Extension extension = new Extension();

List<Tuple<string, string, string>> hand = new List<Tuple<string, string, string>>
{
new Tuple<string, string, string>("K", "5", "3"),
new Tuple<string, string, string>("3", "7", "A")
};

Tuple<string, string, string> winner;

bool result = extension.winningThree(hand, out winner);

Assert.That(result, Is.False);
Assert.That(winner.Item1, Is.EqualTo(String.Empty));
Assert.That(winner.Item2, Is.EqualTo(String.Empty));
Assert.That(winner.Item3, Is.EqualTo(String.Empty));
}

[Test]
public void WinningThree_Scenario2()
{
Extension extension = new Extension();

List<Tuple<string, string, string>> hand = new List<Tuple<string, string, string>>
{
new Tuple<string, string, string>("K", "5", "2"),
new Tuple<string, string, string>("2", "2", "7")
};

Tuple<string, string, string> winner;

bool result = extension.winningThree(hand, out winner);

Assert.That(result, Is.False);
Assert.That(winner.Item1, Is.EqualTo(String.Empty));
Assert.That(winner.Item2, Is.EqualTo(String.Empty));
Assert.That(winner.Item3, Is.EqualTo(String.Empty));
}

[Test]
public void WinningThree_Scenario3()
{
Extension extension = new Extension();

List<Tuple<string, string, string>> hand = new List<Tuple<string, string, string>>
{
new Tuple<string, string, string>("4", "3", "A"),
new Tuple<string, string, string>("6", "6", "6"),
new Tuple<string, string, string>("7", "7", "K"),
new Tuple<string, string, string>("3", "3", "3")
};

Tuple<string, string, string> winner;

bool result = extension.winningThree(hand, out winner);

Assert.That(result, Is.True);
Assert.That(winner.Item1, Is.EqualTo("6"));
Assert.That(winner.Item2, Is.EqualTo("6"));
Assert.That(winner.Item3, Is.EqualTo("6"));
}
}
}