My program needs to have all the combinations of 2s and 0s in a list of lists. Ex: [[0,0,0,2],[0,0,2,0],[0,2,0,0]....]. I will always have n^2 elements in each sub list where there are n-1 times 2s. So I should have n^2!/((n^2-n)!*(n-1)!) results.
The problem is my code first calculates all the permutations, then removes the duplicates. So for n = 4 there will be 16! sublists, which crashes my computer. How can I fix this? (it needs to handle at least n = 8)
Here is the code:
servers = n*n #number of elements in each sublist
infected = n - 1 #number of 2s
grid = [ 0 for a in range(servers)] #list representing grid, with all 0s
grid = grid[:-infected] + infected * [2] #make last ones 2s
all_infections = list(itertools.permutations(grid)) # !!PROBLEM!! create all permutations of infection (touple)
all_infections = [list(a) for a in all_infections] # Convert touple to lists
all_infections.sort()
all_infections = list(k for k,_ in itertools.groupby(all_infections)) #remove duplicates
combinations = len(all_infections)
print (all_infections)
results = []
for index in range(combinations): #calculate the infected states
results = results + [grid_infecter(all_infections[index],n)]