【问题标题】:Need a recursive function to get all permutations of an array where each element is itself plus 0 to n需要一个递归函数来获取数组的所有排列,其中每个元素都是本身加上 0 到 n
【发布时间】:2019-11-25 19:54:58
【问题描述】:

抱歉标题的措辞,因为我不确定如何表达这个问题。

我正在尝试获取数组的所有排列,其中每个元素可能是它的值加上 0 到 n('wild' 值)

例如

The array [0, 1, 0, 2, 1] with the wild value equal to 1 would have the permutations:
[1, 1, 0, 2, 1]
[0, 2, 0, 2, 1]
[0, 1, 1, 2, 1]
[0, 1, 0, 3, 1]
[0, 1, 0, 2, 2]

The array [1, 2, 0, 0] with the wild value equal to 2 would have the permutations:
[3, 2, 0, 0]
[2, 3, 0, 0]
[2, 2, 1, 0]
[2, 2, 0, 1]
[1, 4, 0, 0]
[2, 3, 0, 0]
[1, 3, 1, 0]
[1, 3, 0, 1]
[1, 2, 2, 0]
[2, 2, 1, 0]
[1, 3, 1, 0]
[1, 2, 1, 1]
... and so on...

这是我尝试过的代码,但没有产生预期的结果:

def generateAllMatrices(length, buckets, ind, wild):  

    if ind == length: 
        # possible_buckets.append(buckets.copy())
        print(buckets)
        return

    if wild != 0:
        for i in range(1, wild + 1):
            buckets[ind] += 1
            generateAllMatrices(length, buckets, 0, wild - 1)
        buckets[ind] -= wild

    generateAllMatrices(length, buckets, ind + 1, wild)

上述代码产生的示例结果是:

Original = [1, 0, 0, 2, 0, 1, 0, 0, 0, 1, 1, 0, 0]
Wild = 1

Permutations:
[2, 0, 0, 2, 0, 1, 0, 0, 0, 1, 1, 0, 0]
[1, 1, 0, 2, 0, 1, 0, 0, 0, 1, 1, 0, 0]
[1, 0, 1, 2, 0, 1, 0, 0, 0, 1, 1, 0, 0]
[1, 0, 0, 3, 0, 1, 0, 0, 0, 1, 1, 0, 0]
[1, 0, 0, 2, 1, 1, 0, 0, 0, 1, 1, 0, 0]
[1, 0, 0, 2, 0, 2, 0, 0, 0, 1, 1, 0, 0]
[1, 0, 0, 2, 0, 1, 1, 0, 0, 1, 1, 0, 0]
[1, 0, 0, 2, 0, 1, 0, 1, 0, 1, 1, 0, 0]
[1, 0, 0, 2, 0, 1, 0, 0, 1, 1, 1, 0, 0]
[1, 0, 0, 2, 0, 1, 0, 0, 0, 2, 1, 0, 0]
[1, 0, 0, 2, 0, 1, 0, 0, 0, 1, 2, 0, 0]
[1, 0, 0, 2, 0, 1, 0, 0, 0, 1, 1, 1, 0]
[1, 0, 0, 2, 0, 1, 0, 0, 0, 1, 1, 0, 1]
[1, 0, 0, 2, 0, 1, 0, 0, 0, 1, 1, 0, 0]

有什么类似的算法我可以参考吗?或者我应该采取什么途径来开发能够产生我需要的东西。

谢谢!

【问题讨论】:

    标签: python arrays algorithm recursion


    【解决方案1】:

    您可以执行以下操作:

    import itertools
    
    def make_reps(l, wild):
        for indices in itertools.product(range(len(l)), repeat=wild): 
            new_l = list(l) 
            for i in indices: 
                new_l[i] += 1 
            yield new_l 
    

    用你给出的例子:

    In [12]: list(make_reps([0, 1, 0, 2, 1], 1))                                                    
    Out[12]: 
    [[1, 1, 0, 2, 1],
     [0, 2, 0, 2, 1],
     [0, 1, 1, 2, 1],
     [0, 1, 0, 3, 1],
     [0, 1, 0, 2, 2]]
    
    In [14]: list(make_reps([1, 2, 0, 0], 2))                                                       
    Out[14]: 
    [[3, 2, 0, 0],
     [2, 3, 0, 0],
     [2, 2, 1, 0],
     [2, 2, 0, 1],
     [2, 3, 0, 0],
     [1, 4, 0, 0],
     [1, 3, 1, 0],
     [1, 3, 0, 1],
     [2, 2, 1, 0],
     [1, 3, 1, 0],
     [1, 2, 2, 0],
     [1, 2, 1, 1],
     [2, 2, 0, 1],
     [1, 3, 0, 1],
     [1, 2, 1, 1],
     [1, 2, 0, 2]]
    

    【讨论】:

      猜你喜欢
      • 2020-11-03
      • 1970-01-01
      • 2021-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-12
      相关资源
      最近更新 更多