Permutation in Python using arrays
def perms(X, perm_sample= array([],'uint8') , allPerms = None,ind = 0):
# Returns all permutations and number of permutations
# X = [2 3]
# res,perm_count = ([[0,0],[0,1],[0,2],[1,0],[1,1],[1,2], 6)
if allPerms is None:
#Get memory
size = prod(X)
allPerms = zeros((size,len(X)),'uint8')
if len(X) == 0:
allPerms[ind,:] = perm_sample
perm_sample = array([],'uint8')
ind = ind + 1;
else:
for x in arange(X[0]):
allPerms, ind = perms(X[1:],hstack((perm_sample, [x])), allPerms, ind)
return allPerms, ind
0 Comments:
Post a Comment
<< Home