组合问题


def combination_2(L,k):
    def product2(R,L):
        R1=[]
        if R==[[]]:
            for e in L:R1.append([e])
        else:
            for e in R:
                for f in L:
                    if f not in e and f >e[-1]:
                        R1.append(e+[f])
        return R1
    if k<0 or k> len(L):
        print('k的值应该非负且小于列表长度')
        return []
    if k==0:return [[]]
    R=[[]]
    for i in range(k):
        R=product2(R,L)
    return R


def combination_3(L,k):
    pass

def combination_dfs(L,k):
    R = []
    T = [-1 for i in range(k)]

    def P(L, num_pick, m):
        if m > num_pick - 1:
            R.append(T[:])
        else:
            for i in L:
                if i not in T[0:m]:
                    T[m] = i
                    P(L, num_pick, m + 1)

    P(L, k, 0)
    return R

L = [ 3, 5, 8,2]
# res1=combination_2(L,2)
# print(res1)