Let’s say I have the following arrays of strings:
JavaScript
x
7
1
Background = {"Ocean"}
2
Body = {"Normal"}
3
Eyes = {"Big", "Small", "Monolid"}
4
Color = {"Yellow", "White", "Red Rose", "Turquoise", "Dark green", "Orange"}
5
Hands = {"None", "Robot", "Spider", "Bear"}
6
Extra = {"Empty", "Sand", "Dust", "Graffiti", "Aloe"}
7
I want to print a list that contains all possible permutations of each element mentioned in the arrays above, following the order in which these arrays were set (i.e. it starts checking on Background
, then goes to check Body
, then Eyes
, then Color
, then Hands
, and finishes on Extra
).
The first permutation should be:
JavaScript
1
2
1
1. Ocean, Normal, Big, Yellow, None, Empty
2
The second permutation should be:
JavaScript
1
2
1
2. Ocean, Normal, Big, Yellow, None, Sand
2
And so on…
It can be assumed that the item None
is the same as Empty
.
How could I do that?
Advertisement
Answer
The following solution should work Assume arrays are as follows
JavaScript
1
14
14
1
Backgrounds = ["Ocean"]
2
Bodys = ["Normal"]
3
Eyes = ["Big", "Small", "Monolid"]
4
Colors = ["Yellow", "White", "Red Rose", "Turquoise", "Dark green", "Orange"]
5
Hands = ["None", "Robot", "Spider", "Bear"]
6
Extra = ["Empty", "Sand", "Dust", "Graffiti", "Aloe"]
7
for background in Backgrounds:
8
for body in Bodys:
9
for eye in Eyes:
10
for colour in colours:
11
for hand in Hands:
12
for extra in extras:
13
print(background,body,eye,colour,hand,extra)
14
If your lists are really large please don’t use the above solution because its time complexity is o(n^6).