简体   繁体   English

用于显示列表中所有数字组合的算法

[英]Algorithm to show all combinations of numbers in the list

How would I build a recursive function to show all the possibilities of signs in the list of numbers eg (5, 3, 12, 9, 15). 我如何构建一个递归函数来显示数字列表中所有符号的可能性,例如(5,3,12,9,15)。 The list won't change, just the signs for each number. 列表不会改变,只是每个数字的符号。

For example, the results would be: 例如,结果将是:
(-5, 3, 12, 9, 15) (-5,3,12,9,15)
(-5, -3, 12, 9, 15) (-5,-3,12,9,15)
(-5, -3, -12, 9, 15) (-5,-3,-12,9,15)
(-5, -3, -12, -9, 15) (-5,-3,-12,-9,15)

And so on, until all the combinations of this list are displayed. 依此类推,直到显示此列表的所有组合。

I've tried a few different ways, including trying to adapt code from other similar questions here, but majority of them include changing the list itself. 我尝试了几种不同的方法,包括尝试从其他类似问题调整代码,但其中大多数包括更改列表本身。

Thanks! 谢谢!

Generate all possible 5-elements binary list eg [0,0,0,0,0], [0,0,0,0,1], [0,0,0,1,0] .. [1,1,0,0,1] ... [1,1,1,1,1] . 生成所有可能的5元素二进制列表,例如[0,0,0,0,0], [0,0,0,0,1], [0,0,0,1,0] .. [1,1,0,0,1] ... [1,1,1,1,1] Now, for each of these lists, do the following. 现在,对于每个列表,请执行以下操作。

If there is a 1 in x'th position in the list then replace the number at this position with its negative in your original list. 如果列表中的第x个位置为1,则将原始列表中的负数替换为此位置的数字。

Now the problem is : how to generate all lists of 5 boolean digits recursively (Binary trees?). 现在的问题是:如何递归生成5个布尔数字的所有列表(二叉树?)。

When implementing a recursive function, you need to think about two cases: the base case and the recursive case. 在实现递归函数时,您需要考虑两种情况:基本情况和递归情况。

In the base case, the function doesn't call itself recursively. 在基本情况下,函数不会递归调用自身。 It may do some work in this case, or it may do nothing because all the work has already been done. 在这种情况下它可能会做一些工作,或者它可能什么也不做,因为所有工作都已完成。

In the recursive case, the function does a little bit of work to get itself closer to the goal, then calls itself recursively to get the rest of the work done. 在递归的情况下,函数做了一些工作,使自己更接近目标,然后递归调用自己完成其余的工作。

Here's a way to break down your problem for a recursive function. 这是一种解决递归函数问题的方法。

In the recursive case, the “little bit of work” is setting the sign of one number in the list to positive, and also to set the sign of that number to negative. 在递归的情况下,“一点点工作”是将列表中的一个数字的符号设置为正,并且还将该数字的符号设置为负数。 We need to recurse after both assignments, because we need to generate combinations for each sign. 我们需要在两次分配后递归,因为我们需要为每个符号生成组合。

In the base case, all of the numbers have had their signs set, so we just print the list of numbers. 在基本情况下,所有数字都设置了符号,因此我们只打印数字列表。

In Python, for example, we could start by setting up the function to take a list of numbers, and the index of the next number needing its sign set. 例如,在Python中,我们可以首先设置函数以获取数字列表,以及需要其符号集的下一个数字的索引。 To start, the next number is the first number in the list, at index 0: 首先,下一个数字是列表中的第一个数字,索引为0:

def allSignCombinations(numbers, nextNumberIndex=0):

The base case happens when nextNumberIndex is equal to len(numbers) , meaning there are no numbers left needing their signs set: nextNumberIndex等于len(numbers)时发生基本情况,这意味着没有数字需要设置它们的符号:

    if nextNumberIndex == len(numbers):
        print numbers

Otherwise, we do that “little bit of work”. 否则,我们会做“一点点工作”。 We set the sign of the next number to both positive and negative, and we recurse for each sign. 我们将下一个数字的符号设置为正数和负数,然后我们递归每个符号。 When we recurse, we tell the next call to work starting at the next number in the list, if there is one: 当我们递归时,如果有一个,我们告诉下一个工作从列表中的下一个数字开始工作:

    else:
        numbers[nextNumberIndex] = abs(numbers[nextNumberIndex])
        allSignCombinations(numbers, nextNumberIndex + 1)
        numbers[nextNumberIndex] = -numbers[nextNumberIndex]
        allSignCombinations(numbers, nextNumberIndex + 1)

Building upon Dilawar answer, I offer a (heavily) pythonic implementation (Python language): 在Dilawar的回答基础上,我提供了一个(重度)pythonic实现(Python语言):

numbers = (5, 3, 12, 9, 15)

for n in range(2**len(numbers)):   # for all possible combinations (power of two)
    binrep = bin(n)[2:]            # get the binary representation as string
    binstring = str(binrep).ljust(5,'0')   # pad with left zeros
    binlist = map(int, reversed([c for c in binstring]))  # convert to a list of integers
    # apply element-wise multiplication with transformed (0,1) => (-1,1)
    print [numbers[n] * (binlist[n]*2 -1) for n in range(len(numbers))]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 算法总结所有组合的数字列表 - algorithm to sum up a list of numbers for all combinations 从数字列表中获取所有可能的组合 - Getting all possible combinations from a list of numbers 查找具有给定总和的数字列表的所有组合 - Find all combinations of a list of numbers with a given sum 从给定数字中获取所有可能的操作组合的算法 - Algorithm to get all the possible combinations of operations from a given numbers 该算法将采用数字或单词并找到所有可能的组合 - algorithm that will take numbers or words and find all possible combinations 返回给定数字的所有可能组合的C ++算法 - C++ algorithm that returns all possible combinations of given numbers 从长度为 6 的数组中返回 3 种组合的所有组合的算法,而该数字不会出现在同一组数字中 - Javascript - Algorithm to return all combinations of 3 combinations from a array of length 6 without the number appearing with the same set of numbers - Javascript 有没有更好的算法来为组合分配数字? - Is there a better algorithm to assign numbers to combinations? 从所有组合的假设列表中的索引中获取置换的算法? - Algorithm to get permutation from an index from the hypothetical list of all combinations? Python to C++:使用递归列出所有背包组合的算法 - Python to C++: Algorithm that list all combinations of Knapsack using recursion
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM