Last active
December 2, 2015 19:40
-
-
Save hawx/dea6d8435e36e7e14a1d to your computer and use it in GitHub Desktop.
Suit-blind poker hands kata solution in simple (fors and ifs as much as possible) Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
def winner(hand1, hand2): | |
hand1 = sorted(hand1, reverse=True) | |
hand2 = sorted(hand2, reverse=True) | |
result = try_winner([four_of_a_kind, full_house, straight, three_of_a_kind, | |
two_pairs, pair], hand1, hand2) | |
if result != 0: | |
return result | |
return highcard(hand1, hand2) | |
def try_winner(fs, hand1, hand2): | |
for f in fs: | |
result = score(f, hand1, hand2) | |
if result != 0: | |
return result | |
return 0 | |
def score(f, hand1, hand2): | |
score1 = f(hand1) | |
score2 = f(hand2) | |
if score1 > score2: | |
return 1 | |
if score2 > score1: | |
return 2 | |
return 0 | |
def four_of_a_kind(hand): | |
if hand[0] == hand[1] == hand[2] == hand[3]: | |
return hand[0] | |
if hand[1] == hand[2] == hand[3] == hand[4]: | |
return hand[1] | |
return 0 | |
def full_house(hand): | |
if hand[0] == hand[1] == hand[2] and hand[3] == hand[4]: | |
return hand[0] | |
if hand[0] == hand[1] and hand[2] == hand[3] == hand[4]: | |
return hand[2] | |
return 0 | |
def straight(hand): | |
lastCard = hand[0] | |
for card in hand[1:]: | |
if card != lastCard - 1: | |
return 0 | |
lastCard = card | |
return lastCard | |
def three_of_a_kind(hand): | |
lastCards = [hand[0], hand[1]] | |
for card in hand[2:]: | |
if card == lastCards[0] and card == lastCards[1]: | |
return card | |
lastCards = [lastCards[1], card] | |
return 0 | |
def two_pairs(hand): | |
lastCard = hand[0] | |
pairs = [] | |
for card in hand[1:]: | |
if card == lastCard: | |
pairs.append(card) | |
lastCard = 0 | |
else: | |
lastCard = card | |
if len(pairs) != 2: | |
return 0 | |
return pairs[0] + pairs[1] | |
def pair(hand): | |
lastCard = hand[0] | |
for card in hand[1:]: | |
if card == lastCard: | |
return card | |
lastCard = card | |
return 0 | |
def highcard(hand1, hand2): | |
if len(hand1) == 0: | |
return 0 | |
highestCard1 = hand1[0] | |
highestCard2 = hand2[0] | |
if highestCard1 > highestCard2: | |
return 1 | |
if highestCard2 > highestCard1: | |
return 2 | |
return highcard(hand1[1:], hand2[1:]) | |
class TestPoker(unittest.TestCase): | |
def test_highcard(self): | |
self.assertEqual(winner([9,6,3,2,1], [1,6,3,2,5]), 1) | |
self.assertEqual(winner([1,6,3,2,5], [9,6,3,2,1]), 2) | |
self.assertEqual(winner([1,6,3,2,5], [5,6,3,2,1]), 0) | |
def test_highcard_with_same_highest_card(self): | |
self.assertEqual(winner([9,8,1,2,3], [9,7,1,2,3]), 1) | |
self.assertEqual(winner([9,7,1,2,3], [9,8,1,2,3]), 2) | |
self.assertEqual(winner([9,8,1,2,3], [3,2,1,8,9]), 0) | |
def test_pair(self): | |
self.assertEqual(winner([2,2,1,4,5], [3,5,1,6,7]), 1) | |
self.assertEqual(winner([3,5,1,4,6], [2,2,1,6,7]), 2) | |
self.assertEqual(winner([2,2,1,4,5], [2,2,1,4,5]), 0) | |
self.assertEqual(winner([3,3,1,4,5], [2,2,1,6,7]), 1) | |
self.assertEqual(winner([3,3,1,4,5], [4,4,1,6,7]), 2) | |
def test_two_pairs(self): | |
self.assertEqual(winner([1,1,2,2,7], [1,2,3,4,7]), 1) | |
self.assertEqual(winner([1,2,3,4,7], [1,2,1,2,7]), 2) | |
self.assertEqual(winner([1,1,2,2,7], [1,2,1,2,7]), 0) | |
self.assertEqual(winner([2,4,2,4,7], [1,4,1,4,7]), 1) | |
self.assertEqual(winner([2,3,3,2,7], [1,5,1,5,7]), 2) | |
self.assertEqual(winner([2,3,3,2,7], [1,4,1,4,7]), 2) | |
def test_three_of_a_kind(self): | |
self.assertEqual(winner([1,1,1,2,7], [1,1,2,3,7]), 1) | |
self.assertEqual(winner([1,1,2,3,7], [1,1,1,2,7]), 2) | |
self.assertEqual(winner([1,1,1,2,7], [1,1,1,2,7]), 0) | |
def test_straight(self): | |
self.assertEqual(winner([1,2,3,4,5], [1,2,3,4,6]), 1) | |
self.assertEqual(winner([1,2,3,4,6], [1,2,3,4,5]), 2) | |
self.assertEqual(winner([1,2,3,4,5], [1,2,3,4,5]), 0) | |
self.assertEqual(winner([6,2,3,4,5], [1,2,3,4,5]), 1) | |
self.assertEqual(winner([1,2,3,4,5], [6,2,3,4,5]), 2) | |
def test_full_house(self): | |
self.assertEqual(winner([1,1,2,2,2], [1,2,3,4,5]), 1) | |
self.assertEqual(winner([1,2,3,4,5], [1,1,2,2,2]), 2) | |
self.assertEqual(winner([4,4,3,3,3], [1,2,3,4,5]), 1) | |
def test_four_of_a_kind(self): | |
self.assertEqual(winner([1,1,1,1,2], [3,3,3,2,2]), 1) | |
self.assertEqual(winner([3,3,3,2,2], [1,1,1,1,2]), 2) | |
self.assertEqual(winner([3,3,3,3,2], [4,5,6,7,8]), 1) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment