0

How to find all possible of array switching?

Example

Original Value: [10,5,3,6]

Output:
[10, 5, 3, 6]
[5, 3, 6, 10]
[3, 6, 10, 5]
[6, 10, 5, 3]

[10, 3, 5, 6]
[3, 5, 6, 10]
[5, 6, 10, 3]
[6, 10, 3, 5]

[10, 5, 6, 3]
[5, 6, 3, 10]
[6, 3, 10, 5]
[3, 10, 5, 6]

[10, 6, 5, 3]
[6, 5, 3, 10]
[5, 3, 10, 6]
[3, 10, 6, 5]

[3, 6, 5, 10]
[6, 5, 10, 3]
[5, 10, 3, 6]
[10, 3, 6, 5]

[3, 10, 5, 6]
[10, 5, 6, 3]
[5, 6, 3, 10]
[6, 3, 10, 5]

....

This is not the end of the list, but I am looking for something that can output all combinations of array, given placement should be considered.

For now, I've comeout with something to switch the place


def rotateArray(arr, n, d):
    temp = []
    i = 0
    while (i < d):
        temp.append(arr[i])
        i = i + 1
    i = 0
    while (d < n):
        arr[i] = arr[d]
        i = i + 1
        d = d + 1
    arr[:] = arr[: i] + temp
    return arr

spam = [3, 10, 5, 6]
for i in range(0,len(spam)):
    
    spam = [3, 10, 5, 6]
    a = rotateArray(spam, len(spam), i)
    print(a)

That will switch by 1 place of all value, but not random switching.

Wing Shum
  • 472
  • 6
  • 19

1 Answers1

1

You can use a built-in function:

from itertools import permutations

arr = [10, 5, 3, 6]
print(list(permutations(arr)))
Ratery
  • 2,863
  • 1
  • 5
  • 26