简体   繁体   中英

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). 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)

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] . 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.

Now the problem is : how to generate all lists of 5 boolean digits recursively (Binary trees?).

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. To start, the next number is the first number in the list, at index 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:

    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):

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))]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM