代码拉取完成,页面将自动刷新
"""
Problem 54: https://projecteuler.net/problem=54
In the card game poker, a hand consists of five cards and are ranked,
from lowest to highest, in the following way:
#1. High Card: Highest value card.
#2. One Pair: Two cards of the same value.
#3. Two Pairs: Two different pairs.
#4. Three of a Kind: Three cards of the same value.
#5. Straight: All cards are consecutive values.
#6. Flush: All cards of the same suit.
#7. Full House: Three of a kind and a pair.
#8. Four of a Kind: Four cards of the same value.
#9. Straight Flush: All cards are consecutive values of same suit.
#10. Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.
The cards are valued in the order:
2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace.
If two players have the same ranked hands then the rank made up of the highest
value wins; for example, a pair of eights beats a pair of fives.
But if two ranks tie, for example, both players have a pair of queens, then highest
cards in each hand are compared; if the highest cards tie then the next highest
cards are compared, and so on.
The file, poker.txt, contains one-thousand random hands dealt to two players.
Each line of the file contains ten cards (separated by a single space): the
first five are Player 1's cards and the last five are Player 2's cards.
You can assume that all hands are valid (no invalid characters or repeated cards),
each player's hand is in no specific order, and in each hand there is a clear winner.
How many hands does Player 1 win?
"""
# _*_ conding:UTF-8 _*_
'''
@author = Kuperain
@email = kuperain@aliyun.com
@IDE = VSCODE Python3.8.3
@creat_time = 2022/5/18
'''
N = 5
PokerNums = {
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'T': 10,
'J': 11,
'Q': 12,
'K': 13,
'A': 14,
}
def ranks(cards: list) -> tuple:
'''
return (RoyalFlush, StraightFlush, FourofaKind, FullHouse, Flush,
Straight, ThreeofaKind, TwoPairs, OnePair, HighCard)
>>> print(ranks([(14, 'S'), (13, 'S'), (12, 'S'), (11, 'S'), (10, 'S')] ))
(1, 14, 0, [0, 0], 1, 14, 0, [0, 0], 0, 14)
>>> print(ranks([(9, 'C'), (13, 'S'), (12, 'S'), (11, 'S'), (10, 'S')] ))
(0, 0, 0, [0, 0], 0, 13, 0, [0, 0], 0, 13)
>>> print(ranks([(9, 'C'), (9, 'C'), (9, 'C'), (11, 'C'), (9, 'C')] ))
(0, 0, 9, [0, 0], 1, 0, 9, [0, 0], 9, 11)
'''
cards.sort()
nums = [card[0] for card in cards]
suits = [card[1] for card in cards]
# 1. High Card: Highest value card.
HighCard = max(nums)
# 2. One Pair: Two cards of the same value.
pairs = [cards[i][0]
for i in range(N-1, 0, -1) if cards[i][0] == cards[i-1][0]]
OnePair = pairs[0] if pairs else 0
# 3. Two Pairs: Two different pairs.
if len(pairs) >= 2 and len(set(pairs)) == 2:
TwoPairs = sorted(list(set(pairs)), reverse=True)
else:
TwoPairs = [0, 0]
# 4. Three of a Kind: Three cards of the same value.
triplets = [cards[i][0]
for i in range(N-1, 1, -1) if cards[i] == cards[i-1] == cards[i-2]]
ThreeofaKind = triplets[0] if triplets else 0
# 5. Straight: All cards are consecutive values.
Straight = nums[-1] if nums == list(range(nums[0], nums[0]+N)) else 0
# 6. Flush: All cards of the same suit.
Flush = 1 if len(set(suits)) == 1 else 0
# 7. Full House: Three of a kind and a pair.
if ThreeofaKind and OnePair != ThreeofaKind:
FullHouse = [ThreeofaKind, OnePair]
else:
FullHouse = [0, 0]
# 8. Four of a Kind: Four cards of the same value.
FourofaKind = triplets[0] if len(triplets) >= 2 else 0
# 9. Straight Flush: All cards are consecutive values of same suit.
StraightFlush = Straight if Straight and Flush else 0
# 10. Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.
RoyalFlush = 1 if StraightFlush == 14 else 0
return (RoyalFlush, # 10
StraightFlush, # 9
FourofaKind, # 8
FullHouse, # 7
Flush, # 6
Straight, # 5
ThreeofaKind, # 4
TwoPairs, # 3
OnePair, # 2
HighCard) # 1
def whowin(cards1, cards2):
if ranks(cards1) > ranks(cards2):
return 1
if ranks(cards1) < ranks(cards2):
return -1
raise ValueError(f'Any of {cards1} VS {cards2} can not win!')
def solution(roundsfile: str = 'p054_poker_hands.txt') -> int:
count = 0
with open(roundsfile, 'r') as rs:
# while True:
for round in rs:
# round = rs.readline()
# if not round:
# break
cards = eval("['"+round.strip().replace(' ', "','")+"']")
cards = [(PokerNums[card[0]], card[1]) for card in cards]
cards1, cards2 = cards[:5], cards[5:]
if whowin(cards1, cards2) == 1:
count += 1
return count
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=False)
print(solution())
# 380
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。