|
发表于 2016-10-14 16:32:33
|
显示全部楼层
这题不是难,是很烦,要考虑很多种情况,写不同条件.
帖子长度关系,省略牌组数据,可以自行粘贴。
376
[Finished in 0.2s]
- pokerdata = ['8C','TS','KC','9H','4S','7D','2S','5D','3S','AC','5C','AD','5D','AC','9C','7C','5H','8D','TD','KS','3H','7H','6S','KC','JS','QH','TD','JC','2D','8S','TH','8H',
- ######################
- ######################
- ###~~~省略牌组数据~~~###
- ######################
- ######################
- '2D','JS','QD','AC','9C','JD','7C','6D','TC','6H','6C','JC','3D','3S','QC','KC','3S','JC','KD','2C','8D','AH','QS','TS','AS','KD','3D','JD','8H','7C','8C','5C','QD','6C']
- def evalHand(hand):
- values = ['2','3','4','5','6','7','8','9','T','J','Q','K','A']
- flush = 1
- suit = hand[0][1]
- for card in hand:
- if card[1] <> suit:
- flush = 0
- break
- indices = []
- royal = 1
- straight = 1
- for card in hand:
- indices.append(values.index(card[0]))
- indices.sort()
- if indices[4] - indices[0] <> 4 \
- or indices.count(indices[0]) > 1 \
- or indices.count(indices[1]) > 1 \
- or indices.count(indices[2]) > 1 \
- or indices.count(indices[3]) > 1 \
- or indices.count(indices[4]) > 1:
- straight = 0
- if indices[0] <> 8:
- royal = 0
- kinds = []
- for value in indices:
- count = indices.count(value)
- if count > 1:
- kind = [value, count]
- if kind not in kinds:
- kinds.append(kind)
- if royal and flush:
- # return "royal flush"
- return [9, 0]
- if straight and flush:
- # return "straight flush"
- return [8, indices[4]]
- if len(kinds) == 1 and kinds[0][1] == 4:
- # return "four of a kind"
- return [7, kinds[0][0]]
- if len(kinds) == 2 and (kinds[0][1] + kinds[1][1] == 5):
- # return "full house"
- return [6, kinds[0][0]]
- if flush:
- # return "flush"
- return [5, indices[4]]
- if straight:
- # return "straight"
- return [4, indices[4]]
- if len(kinds) == 1 and kinds[0][1] == 3:
- # return "three of a kind"
- return [3, kinds[0][0]]
- if len(kinds) == 2 and (kinds[0][1] + kinds[1][1] == 4):
- # return "two pair"
- return [2, max(kinds[0][0], kinds[1][0])]
- if len(kinds) == 1 and kinds[0][1] == 2:
- # return "one pair"
- return [1, kinds[0][0]]
- # return "high card"
- return [0, max(indices)]
- rounds = []
- for rd in range(1000):
- rounds.append(pokerdata[rd*10:rd*10+10])
- count = 0
- for r in rounds:
- p1 = evalHand(r[0:5])
- p2 = evalHand(r[5:10])
- if p1[0] > p2[0] or (p1[0] == p2[0] and p1[1] > p2[1]):
- count = count + 1
- print count
复制代码 |
|