栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

扑克手评估的最简单算法

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

扑克手评估的最简单算法

这是Python(2.x)中非常简短但完整的基于直方图的5张纸牌扑克评分功能。如果转换为Java,它将变得更长。

def poker(hands):    scores = [(i, score(hand.split())) for i, hand in enumerate(hands)]    winner = sorted(scores , key=lambda x:x[1])[-1][0]    return hands[winner]def score(hand):    ranks = '23456789TJQKA'    rcounts = {ranks.find(r): ''.join(hand).count(r) for r, _ in hand}.items()    score, ranks = zip(*sorted((cnt, rank) for rank, cnt in rcounts)[::-1])    if len(score) == 5:        if ranks[0:2] == (12, 3): #adjust if 5 high straight ranks = (3, 2, 1, 0, -1)        straight = ranks[0] - ranks[4] == 4        flush = len({suit for _, suit in hand}) == 1        '''no pair, straight, flush, or straight flush'''        score = ([1, (3,1,1,1)], [(3,1,1,2), (5,)])[flush][straight]    return score, ranks >>> poker(['8C TS KC 9H 4S', '7D 2S 5D 3S AC', '8C AD 8D AC 9C', '7C 5H 8D TD KS']) '8C AD 8D AC 9C'


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/398179.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号