combinations - Python Poker hand single pair counter -
i wrote program below iterate through every possible poker hand , count how many of these hands single pair
a hand 5 cards.
single pair when 2 cards of same rank (number) , other 3 cards of different ranks e.g. (1,2,1,3,4)
i representing deck of cards list of numbers e.g.
- 1 = ace
- 2 = two
- 3 = three
...
- 11 = jack
- 12 = queen...
the program seems work find however, number of single pair hands finds = 1101984
but according multiple sources correct answer 1098240.
can see error in code is?
from itertools import combinations # generating deck deck = [] in range(52): deck.append(i%13 + 1) def paircount(hand): paircount = 0 in hand: count = 0 x in hand: if x == i: count += 1 if count == 2: paircount += .5 #adding 0.5 because each pair counted twice return paircount count = 0 in combinations(deck, 5): # loop through combinations of 5 if paircount(i) == 1: count += 1 print(count)
the issue hand can contain following type of cards -
a 3 of kind , single pair
you calculating single pair well.
i modified code count number of hands such contains 3 of kind single pair together. code -
deck = [] in range(52): deck.append((i//13 + 1, i%13 + 1)) def paircount(hand): paircount = 0 threecount = 0 in hand: count = 0 x in hand: if x[1] == i[1]: count += 1 if count == 2: paircount += .5 #adding 0.5 because each pair counted twice if count == 3: threecount += 0.33333333 return (round(paircount, 0) , round(threecount, 0)) count = 0 in combinations(deck, 5): if paircount(i) == (1.0, 1.0): count += 1 this counted number - 3744.
now, if subtract number number got - 1101984 - number expecting - 1098240 .
Comments
Post a Comment