首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python扑克手单对计数器

Python扑克手单对计数器
EN

Stack Overflow用户
提问于 2015-07-13 11:15:58
回答 1查看 1.3K关注 0票数 2

我编写了下面的程序来迭代每一只可能的扑克手,并计算出其中有多少只手是一双。

一只手就是任何五张牌。

单对牌是指同一等级的两张牌(数)和其他三张不同等级的牌,例如(1、2、1、3、4)。

我把这副牌代表成一个数字列表。

  • 1= ACE
  • 2=2
  • 3=3 ..。
  • 11 = Jack
  • 12 =女王..。

然而,它找到的单双手数= 1101984

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

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

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-13 11:43:49

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

一双三只,一双一双

你实际上也在把这算成一对。

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

代码语言:javascript
复制
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中减去这个数字。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31381901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档