Jun-13-2017, 10:26 PM
I'm writing code to find all solutions of a commercial, physical puzzle. This is done by having the script go through all possible configurations/permutations, check to see if it works, and record the results into a text file.
I've broken down that puzzle into 9 pieces (represented by the list), with each piece having 4 states (NOT represented). Part of the algorithm is to evaluate all possible permutations of 9 pieces across all 4 states.
For example...
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 2
... and then eventually to
0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 1 2
etc.
The code I have to go through all combinations of all 9 elements, with each element having 4 different states is below.
QUESTION 1)
Are there alternative ways to go about this besides a 9-level deep, nested for loop, with each loop going to a max of 4?
MIDEDIT: I've been made aware of itertools, but will need to come back to it later after I have the chance to look through it
ht tps://docs.python.org/3/library/itertools.html
Not only do I need to do the above, but I need to do the above for each combination of placements in the list. For example, I also have to repeat that code with the list as:
[a, b, c, d, e, f, g, i, h]
For this, I'm using Heap's Algorithm, which looks like it's n factorial iterations!
ht tps://en.wikipedia.org/wiki/Heap%27s_algorithm
I'm not sure how practical it is for me to run through this, as it looks like each check gets done...
9! * (4^9)
362,880 * 262,144
87,869,214,720 total times!
I was going to leave this running overnight, but it looks like this may take a few days, or beyond! I want to make sure it's something that won't take too much time to solve.
I've broken down that puzzle into 9 pieces (represented by the list), with each piece having 4 states (NOT represented). Part of the algorithm is to evaluate all possible permutations of 9 pieces across all 4 states.
For example...
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 2
... and then eventually to
0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 1 2
etc.
The code I have to go through all combinations of all 9 elements, with each element having 4 different states is below.
QUESTION 1)
Are there alternative ways to go about this besides a 9-level deep, nested for loop, with each loop going to a max of 4?
MIDEDIT: I've been made aware of itertools, but will need to come back to it later after I have the chance to look through it
ht tps://docs.python.org/3/library/itertools.html
l = [a, b, c, d, e, f, g, h, i] for i1 in range(4): for i2 in range(4): for i3 in range(4): #and then 6 more levels deepQUESTION 2) What's the runtime? Are my calculations correct?
Not only do I need to do the above, but I need to do the above for each combination of placements in the list. For example, I also have to repeat that code with the list as:
[a, b, c, d, e, f, g, i, h]
For this, I'm using Heap's Algorithm, which looks like it's n factorial iterations!
ht tps://en.wikipedia.org/wiki/Heap%27s_algorithm
I'm not sure how practical it is for me to run through this, as it looks like each check gets done...
9! * (4^9)
362,880 * 262,144
87,869,214,720 total times!
I was going to leave this running overnight, but it looks like this may take a few days, or beyond! I want to make sure it's something that won't take too much time to solve.