From 7ab229ba18b0496551d420290de3a317cf6c1bd3 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Fri, 23 Apr 2010 03:55:50 +0000 Subject: [PATCH] Corrected: Values not sorted properly for hands with multiple multiples. Added a test case to handle it. git-svn-id: file:///srv/svn/euler@52 e5f4c3ec-3c0c-11df-b522-21efaa4426b5 --- p054/poker.py | 2 +- p054/test.py | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/p054/poker.py b/p054/poker.py index 6b7d6af..dcb4684 100644 --- a/p054/poker.py +++ b/p054/poker.py @@ -174,7 +174,7 @@ class Hand: self.__rank = Hand.THREE_OF_A_KIND else: self.__rank = Hand.ONE_PAIR - mvalues = [m[0] for m in multiples] + mvalues = sorted([m[0] for m in multiples], reverse=True) self.__values = mvalues + [c.value for c in self.__cards if c.value not in mvalues] if not self.__rank: self.__rank = Hand.HIGH_CARD diff --git a/p054/test.py b/p054/test.py index c8abce8..5195195 100644 --- a/p054/test.py +++ b/p054/test.py @@ -46,6 +46,10 @@ class TestFiveCardHands(unittest.TestCase): low = poker.Hand(['AH', '2S', '3C', '4S', '5S']) high = poker.Hand(['2S', '3C', '4S', '5S', '6S']) self.assertTrue(low < high) + def test_compare_two_pair(self): + low = poker.Hand(['7S', '9D', 'JH', '7D', 'JS']) + high = poker.Hand(['AS', 'AD', '5C', '2D', '2H']) + self.assertTrue(low < high) def test_compare_ranks(self): for rank, hand in self.rank_hands.iteritems(): for rank2, hand2 in self.rank_hands.iteritems(): @@ -83,6 +87,10 @@ class TestSevenCardHands(unittest.TestCase): low = poker.Hand.create_best_hand(['AH', '2S', '3C', '4S', '9D', '4C', '5S']) high = poker.Hand.create_best_hand(['2S', '3C', '4S', '5S', '8D', 'TC', '6S']) self.assertTrue(low < high) + def test_compare_two_pair(self): + low = poker.Hand.create_best_hand(['3D', 'KC', '7S', '9D', 'JH', '7D', 'JS']) + high = poker.Hand.create_best_hand(['4D', '9H', 'AS', 'AD', '5C', '2D', '2H']) + self.assertTrue(low < high) def test_compare_ranks(self): for rank, hand in self.rank_hands.iteritems(): for rank2, hand2 in self.rank_hands.iteritems(): @@ -94,4 +102,4 @@ class TestSevenCardHands(unittest.TestCase): self.assertTrue(hand.rank() > hand2.rank(), '{0} > {1}'.format(poker.Hand.RANKS[rank], poker.Hand.RANKS[rank2])) if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main()