diff --git a/054/054.py b/054/e054.py similarity index 77% rename from 054/054.py rename to 054/e054.py index 6ee369a..9e0bdce 100644 --- a/054/054.py +++ b/054/e054.py @@ -1,5 +1,10 @@ import operator +class InvalidCard(Exception): + pass +class InvalidHand(Exception): + pass + class PokerCard: values = {'T': 10, 'J': 11, 'Q': 12, 'K': 13, 'A': 14} def __init__(self, string): @@ -7,8 +12,13 @@ class PokerCard: if self.value in '23456789': self.value = int(self.value) else: - self.value = PokerCard.values[self.value] + try: + self.value = PokerCard.values[self.value] + except KeyError as e: + raise InvalidCard self.suit = string[1] + if not self.suit in ['H', 'C', 'S', 'D']: + raise InvalidCard @staticmethod def compare(a, b): if a.value > b.value: @@ -118,28 +128,29 @@ class PokerHand: return result return result -wins = 0 -counter = 0 -with open('poker.txt', 'r') as f: - while True: - line = f.readline() - if not line: - break - counter = counter + 1 - cards = line.strip().split() - one = PokerHand(cards[:5]) - two = PokerHand(cards[-5:]) - result = PokerHand.compare(one, two) - if result > 0: - wins = wins + 1 - outcome = 'Player One wins' - elif result == 0: - outcome = 'Tie' - else: - outcome = 'Player Two wins' - print "Hand #{0}: {1}\n {2}\n {3}".format( - counter, - outcome, - one, - two) -print "Player one won {0} hands".format(wins) +if __name__ == '__main__': + wins = 0 + counter = 0 + with open('poker.txt', 'r') as f: + while True: + line = f.readline() + if not line: + break + counter = counter + 1 + cards = line.strip().split() + one = PokerHand(cards[:5]) + two = PokerHand(cards[-5:]) + result = PokerHand.compare(one, two) + if result > 0: + wins = wins + 1 + outcome = 'Player One wins' + elif result == 0: + outcome = 'Tie' + else: + outcome = 'Player Two wins' + print "Hand #{0}: {1}\n {2}\n {3}".format( + counter, + outcome, + one, + two) + print "Player one won {0} hands".format(wins) diff --git a/054/test.py b/054/test.py new file mode 100644 index 0000000..0a4e08d --- /dev/null +++ b/054/test.py @@ -0,0 +1,28 @@ +from e054 import * +import unittest + +class TestCards(unittest.TestCase): + def setUp(self): + pass + def test_valid_number_card(self): + card = PokerCard('9D') + self.assertEqual(card.value, 9) + def test_valid_face_card(self): + card = PokerCard('QH') + self.assertEqual(card.value, 12) + def test_invalid_card_value(self): + self.assertRaises(InvalidCard, PokerCard, 'ZH') + def test_invalid_card_suit(self): + self.assertRaises(InvalidCard, PokerCard, '9Z') + def test_compare(self): + cards = ['QH', '9D', 'JS'] + cards_sorted = sorted([PokerCard(c) for c in cards], cmp=PokerCard.compare, reverse=True) + self.assertEqual([c.value for c in cards_sorted], [12, 11, 9]) + +class TestHands(unittest.TestCase): + def setUp(self): + pass + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file