【发布时间】:2019-06-13 15:29:58
【问题描述】:
我在 Udacity 学习 CS212,代码如下。
使用说明: 编写一个函数 best_wild_hand(hand) ,它将作为 输入 7 张牌并返回最好的 5 张牌。 在这个问题中,一手牌可能包括 小丑。小丑将被视为“外卡” 可以采取任何等级或相同颜色的套装。这 黑色小丑,“?B”,可用作任何黑桃或梅花 红色小丑“?R”可以用作任何心形 或钻石。
代码如下:
import itertools
def best_wild_hand(hand):
"Try all values for jokers in all 5-card selections."
if '?B' not in hand and '?R' not in hand:
return max(itertools.combinations(hand,5), key=hand_rank)
black = [ i+j for i in list('23456789TJQKA') for j in ['S','C']]
red = [ i+j for i in list('23456789TJQKA') for j in ['H','D']]
cards = []
hands = []
if '?B' in hand and '?R' not in hand:
for item in black:
temp = hand[:]
temp[temp.index('?B')] = item
cards.append(temp)
elif '?R' in hand and '?B' not in hand:
for item in red:
temp = hand[:]
temp[temp.index('?R')] = item
cards.append(temp)
else:
for i in black:
for j in red:
temp = hand[:]
temp[temp.index('?B')] = i
temp[temp.index('?R')] = j
cards.append(temp)
#cards = set(cards)
for card in cards:
hands += itertools.combinations(card, 5)
#print(len(hands))
#hands = set(hands)
#print(len(hands))
return max(hands, key=hand_rank)
def test_best_wild_hand():
assert (sorted(best_wild_hand("6C 7C 8C 9C TC 5C ?B".split()))
== ['7C', '8C', '9C', 'JC', 'TC'])
assert (sorted(best_wild_hand("TD TC 5H 5C 7C ?R ?B".split()))
== ['7C', 'TC', 'TD', 'TH', 'TS'])
assert (sorted(best_wild_hand("JD TC TH 7C 7D 7S 7H".split()))
== ['7C', '7D', '7H', '7S', 'JD'])
return 'test_best_wild_hand passes'
# ------------------
# Provided Functions
#
# You may want to use some of the functions which
# you have already defined in the unit to write
# your best_hand function.
def hand_rank(hand):
"Return a value indicating the ranking of a hand."
ranks = card_ranks(hand)
if straight(ranks) and flush(hand):
return (8, max(ranks))
elif kind(4, ranks):
return (7, kind(4, ranks), kind(1, ranks))
elif kind(3, ranks) and kind(2, ranks):
return (6, kind(3, ranks), kind(2, ranks))
elif flush(hand):
return (5, ranks)
elif straight(ranks):
return (4, max(ranks))
elif kind(3, ranks):
return (3, kind(3, ranks), ranks)
elif two_pair(ranks):
return (2, two_pair(ranks), ranks)
elif kind(2, ranks):
return (1, kind(2, ranks), ranks)
else:
return (0, ranks)
def card_ranks(hand):
"Return a list of the ranks, sorted with higher first."
ranks = ['--23456789TJQKA'.index(r) for r, s in hand]
ranks.sort(reverse = True)
return [5, 4, 3, 2, 1] if (ranks == [14, 5, 4, 3, 2]) else ranks
def flush(hand):
"Return True if all the cards have the same suit."
suits = [s for r,s in hand]
return len(set(suits)) == 1
def straight(ranks):
"""Return True if the ordered
ranks form a 5-card straight."""
return (max(ranks)-min(ranks) == 4) and len(set(ranks)) == 5
def kind(n, ranks):
"""Return the first rank that this hand has
exactly n-of-a-kind of. Return None if there
is no n-of-a-kind in the hand."""
for r in ranks:
if ranks.count(r) == n: return r
return None
def two_pair(ranks):
"""If there are two pair here, return the two
ranks of the two pairs, else None."""
pair = kind(2, ranks)
lowpair = kind(2, list(reversed(ranks)))
if pair and lowpair != pair:
return (pair, lowpair)
else:
return None
print(test_best_wild_hand())
上面的代码可以正常工作。但是,当我将 def best_wild_hand(hand) 中的最后一行更改为
return max(set(hands), key=hand_rank)
结果是错误的。为什么我将list 转换为set 而max() 不起作用?谢谢
【问题讨论】:
-
ok 首先,如果你想在一个列表中找到最大值,不需要使用 set(),其次,我们需要知道列表“hands”里面是什么(它是一个元组列表? 字典列表?) 否则无法提供帮助。
-
@Inspi,我转为
set,因为hands可能有重复元素。hands也是一个元组列表,例如:[('6C', '7C', '8C', '9C', 'TC'), ('6C', '7C', '8C', '9C', '5C')]。谢谢 -
我很确定使用 set() 会比保持列表原样慢,即使有重复,你确定函数 hand_rank 像你打算的那样工作吗?
-
@Inspi,是的,
hand_rank是由 udacity 提供的。我很惊讶将list转换为map,max()不起作用。 -
你还应该检查一下手上是否装满了你想要的手
标签: python-3.x list set max