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
This commit is contained in:
Correl Roush 2010-04-23 03:55:50 +00:00
parent ad80b41e06
commit 7ab229ba18
2 changed files with 10 additions and 2 deletions

View file

@ -174,7 +174,7 @@ class Hand:
self.__rank = Hand.THREE_OF_A_KIND self.__rank = Hand.THREE_OF_A_KIND
else: else:
self.__rank = Hand.ONE_PAIR 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] self.__values = mvalues + [c.value for c in self.__cards if c.value not in mvalues]
if not self.__rank: if not self.__rank:
self.__rank = Hand.HIGH_CARD self.__rank = Hand.HIGH_CARD

View file

@ -46,6 +46,10 @@ class TestFiveCardHands(unittest.TestCase):
low = poker.Hand(['AH', '2S', '3C', '4S', '5S']) low = poker.Hand(['AH', '2S', '3C', '4S', '5S'])
high = poker.Hand(['2S', '3C', '4S', '5S', '6S']) high = poker.Hand(['2S', '3C', '4S', '5S', '6S'])
self.assertTrue(low < high) 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): def test_compare_ranks(self):
for rank, hand in self.rank_hands.iteritems(): for rank, hand in self.rank_hands.iteritems():
for rank2, hand2 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']) 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']) high = poker.Hand.create_best_hand(['2S', '3C', '4S', '5S', '8D', 'TC', '6S'])
self.assertTrue(low < high) 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): def test_compare_ranks(self):
for rank, hand in self.rank_hands.iteritems(): for rank, hand in self.rank_hands.iteritems():
for rank2, hand2 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])) self.assertTrue(hand.rank() > hand2.rank(), '{0} > {1}'.format(poker.Hand.RANKS[rank], poker.Hand.RANKS[rank2]))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()