Python牌单对牌

2024-04-19 02:31:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了下面的程序,迭代每一个可能的扑克手,并计算有多少手是一对

一手牌是任意5张牌。
单对是指两张牌的等级(编号)相同,其他三张牌的等级不同,例如(1,2,1,3,4)

我将一副牌表示为数字列表,例如
-1=ACE
-2=两个
-3=三

-11=插孔
-12=大号。。。在

这个程序看起来很管用, 它找到的单对手的数量=1101984

但根据多方消息来源,正确答案是1098240。在

有人能看到我代码中的错误在哪里吗?在

from itertools import combinations
# Generating the deck
deck = []
for i in range(52):
    deck.append(i%13 + 1)

def pairCount(hand):
    paircount = 0
    for i in hand:
        count = 0
        for x in hand:
            if x == i:
                count += 1
        if count == 2:
            paircount += .5 #Adding 0.5 because each pair is counted twice

    return paircount

count = 0
for i in combinations(deck, 5): # loop through all combinations of 5
    if pairCount(i) == 1:
        count += 1

print(count)

Tags: in程序列表forifcount数字编号
1条回答
网友
1楼 · 发布于 2024-04-19 02:31:26

问题是你的手牌也可以包含以下类型的牌-

A Three of a kind and a single pair

实际上,你也把它算作一对。在

我修改了代码,只计算手的数量,这样它就包含了三个一类的以及一对一对的。代码-

deck = []
for i in range(52):
    deck.append((i//13 + 1, i%13 + 1))

def pairCount(hand):
    paircount = 0
    threecount = 0
    for i in hand:
        count = 0
        for x in hand:
            if x[1] == i[1]:
                count += 1
        if count == 2:
            paircount += .5 #Adding 0.5 because each pair is counted twice
        if count == 3:
            threecount += 0.33333333
    return (round(paircount, 0) , round(threecount, 0))

count = 0
for i in combinations(deck, 5):
    if pairCount(i) == (1.0, 1.0):
        count += 1

这将数字计数为-3744。在

现在,如果我们从你得到的数字中减去这个数字-1101984-我们就得到了你想要的数字-1098240。在

相关问题 更多 >