I need to make a data structure of permutations. Currently I am using a generator that is very time expensive. Is there an alternative to a generator or some other way to systematically step through different indices in for a matrix? The other problem might be the function that now takes the strings and makes them a list of lists.
This is for an assignment problem.
def ourpermutations(iterable, r=None):
"""
Input:
String or numbers separated by a space
optional= the length that the permutations must be
Output:
a generator of permutations
"""
pool = iterable.split(" ")
n = len(pool)
r = n if r is None else r
for indices in product(range(n), repeat=r):
if len(set(indices)) == r:
yield tuple(pool[i] for i in indices)
def ExhaustiveSearchinputs(datamatrix):
"""
Input:
datamatrix: numpy array
Output:
list of every permutation allowed and the time it took to run(this is to help with the optimisation and
testing process)
"""
# Important starting values
start = time.time()
length = len(datamatrix)
thestring = ""
#Generate the permutations
for i in range(0,length): #this is making a string of numbers from 0 to the size of the matrix -1
thestring += str(i) + " "
thestring = thestring[:-1]
listofassociations = list(ourpermutations(thestring,length)) #this was the function we made earlier
#these are the time calculation
end = time.time()
thetime = end - start
return listofassociations, thetime,thestring
##########PLEASE NOTE THIS FUNCTION TAKES 4 seconds once datamatrix is length 8 and takes 99 seconds for length 9
The output is correct just slow.