python定义⼀个列表存放52张扑克牌_⽣成所有5张扑克牌你的整体⽅法是正确的。我确信问题出在你的make_canonical函数上。你可以试着⽤数字卡打印出来,设置为3或4,寻找你错过的等价物。
我找到了⼀个,但可能还有更多:# The inputs are equivalent and should return the same value
print make_canonical([8, 12 | 1]) # returns [8, 13]
print make_canonical([12, 8 | 1]) # returns [12, 9]
作为参考,下⾯是我的解决⽅案(在查看您的解决⽅案之前开发)。我⽤深度优先搜索代替⼴度优先搜索。另外,我编写了⼀个函数来检查⼿是否是规范的,⽽不是编写⼀个将⼿转换为规范形式的函数。如果它不规范,我就跳过它。我定义了等级=卡%13和套装=卡/13。这些差异都不重要。import collections
hmo>幼儿园教师演讲稿def canonical(cards):
vuvuzela"""
Rules for a canonical hand:
1. The cards are in sorted order
2. The i-th suit must have at least many cards as all later suits. If a
suit isn't prent, it counts as having 0 cards.
3. If two suits have the same number of cards, the ranks in the first suit
must be lower or equal lexicographically (e.g., [1, 3] <= [2, 4]).
killer
每天都是一首诗4. Must be a valid hand (no duplicate cards)
"""
if sorted(cards) != cards:
return Fal
by_suits = collections.defaultdict(list)
for suit in range(0, 52, 13):
by_suits[suit] = [card%13 for card in cards if suit <= card < suit+13]
if len(t(by_suits[suit])) != len(by_suits[suit]):
return Fal
for suit in range(13, 52, 13):
suit1 = by_suits[suit-13]
什么是同位语suit2 = by_suits[suit]
if not suit2: continue
if len(suit1) < len(suit2):
56upreturn Fal
if len(suit1) == len(suit2) and suit1 > suit2:
return Fal
return True
def deal_cards(permutations, n, cards):
if len(cards) == n:
fort mcmurray
permutations.append(list(cards))
return
start = 0
memaif cards:
start = max(cards) + 1
for card in range(start, 52):
cards.append(card)
if canonical(cards):
deal_cards(permutations, n, cards)
del cards[-1]
def generate_permutations(n):
permutations = []
deal_cards(permutations, n, [])
return permutations
for cards in generate_permutations(5):
print cards
它⽣成正确的排列数:Cashew:~/$ python2.6 /tmp/cards.py | wc 134459
语文高考试卷