我的程序的递归?

时间:2014-05-20 13:09:01

标签: python recursion

这段代码的作用是打印出可以播放的所有n种组合。 S = SpadesH = HeartsD = DiamondsC = Clubs的位置。所以在这种情况下,手会产生:

[['3H'], ['3H', '3C'], ['3H', '3D'], ['3H', '3C', '3D'], ['3C'], ['3C', '3D'], ['3D'], ['4S'], ['6D'], ['7D'], ['9S']]

所有可玩的n种组合。

我想知道我是否有办法在递归循环中执行这段代码?如果在牌组中有超过4套西装,那么继续重新输入迭代将会很繁琐

hand = ['3H', '3C', '3D', '4S', '6D', '7D', '9S']

def generate_plays(sorted_hand_value):
    playable_card = []
    for i in range(len(hand)):  
        playable_card.append([sorted_hand_value[i]]) # appends 1 of a kind to the     playable_cards list

    if i+3 <= (len(hand)-1): #need this restriction of that the interation won't index something that is out of the range of the list

        if sorted_hand_value[i][0] == sorted_hand_value[i+1][0]: #checks if first and second card have the same value
            playable_card.append(list((sorted_hand_value[i],sorted_hand_value[i+1]))) #appends 2 of a kind to the playable_card list

            if sorted_hand_value[i][0] == sorted_hand_value[i+2][0]:
                playable_card.append(list((sorted_hand_value[i],sorted_hand_value[i+2]))) #checks if first and third card have the same value
                playable_card.append(list((sorted_hand_value[i],sorted_hand_value[i+1],sorted_hand_value[i+2]))) #appends 3 of a kind to the playable_card list 

                if sorted_hand_value[i][0] == sorted_hand_value[i+3][0]:
                    playable_card.append(list((sorted_hand_value[i],sorted_hand_value[i+3])))#checks if first and fourth card have the same value
                    playable_card.append(list((sorted_hand_value[i],sorted_hand_value[i+1],sorted_hand_value[i+2],sorted_hand_value[i+3]))) #appends 4 of a kind to the playable_card list

    elif i+2 <= (len(hand)-1):

        if sorted_hand_value[i][0] == sorted_hand_value[i+1][0]:
            playable_card.append(list((sorted_hand_value[i],sorted_hand_value[i+1])))

            if sorted_hand_value[i][0] == sorted_hand_value[i+2][0]:
                playable_card.append(list((sorted_hand[i],sorted_hand[i+2])))
                playable_card.append(list((sorted_hand_value[i],sorted_hand_value[i+1],sorted_hand_value[i+2])))

    elif i+1 <= (len(hand)-1):

        if sorted_hand_value[i][0] == sorted_hand_value[i+1][0]:
            playable_card.append(list((sorted_hand_value[i],sorted_hand_value[i+1])))
return playable_card

print generate_plays(hand) # 

2 个答案:

答案 0 :(得分:1)

from collections import defaultdict
from itertools import combinations

hand = ['3H', '3C', '3D', '4S', '6D', '7D', '9S']

def all_combos(cards):
    # return all non-empty combinations in ascending order by number of items
    for howmany in range(1, len(cards)+1):
        for combo in combinations(cards, howmany):
            yield combo

def n_of_a_kind(hand):
    # get suits for each value
    # ie {'3': ['3H', '3C', '3D'], '4': ['4S'], '6': ['6D'], '7': ['7D'], '9': ['9S']}
    value_suits = defaultdict(list)
    for card in hand:
        value_suits[card[0]].append(card)
    # get all possible non-empty combinations for each value
    return [combo for cards in value_suits.values() for combo in all_combos(cards)]

然后

print(n_of_a_kind(hand))

给出

[('4S',), ('7D',), ('6D',), ('9S',), ('3H',), ('3C',), ('3D',), ('3H', '3C'), ('3H', '3D'), ('3C', '3D'), ('3H', '3C', '3D')]

答案 1 :(得分:0)

您可以使用groupby选择具有相同值的卡片组(首先对它们进行排序,以确保它们全部在一起),然后使用powerset配方生成所有n-a-kind (不包括空集)。

from itertools import groupby, combinations

def kinds(cards):
    for _, cs in groupby(sorted(cards), lambda x: x[0]):
        s = list(cs)
        for r in xrange(1, len(s)+1):
            for p in combinations(s, r):
                yield list(p)

print list(kinds(['3H', '3C', '3D', '4S', '6D', '7D', '9S']))

输出结果为:

[['3C'], ['3D'], ['3H'], ['3C', '3D'], ['3C', '3H'], ['3D', '3H'],
 ['3C', '3D', '3H'], ['4S'], ['6D'], ['7D'], ['9S']]