arrays - Permutations in Ruby - unsure how to handle -
i trying output array of 10-element arrays containing permutations of 1 , 2, e.g.:
[[1,1,1,1,1,1,1,1,1,1], [1,1,1,1,1,1,1,1,1,2], [1,2,1,2,1,2,1,2,1,2], ...etc... [2,2,2,2,2,2,2,2,2,2]]
i have done smaller arrays, (10):
= [1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2] = a.permutation(10).to_a print a.uniq
...this apparently big calculation- after hour running, hadn't completed , ruby process sitting on 12gb of memory. there way come @ this?
first, check size of permutation
a.permutation(10).size => 670442572800
it's huge. can instead use array#repeated_permutations
on smaller array. check here:
b = [1, 2] # unique elements b.repeated_permutation(10) # returns enumerable b.repeated_permutation(10).to_a # creates array of permutations
those permutations unique (which u can check printing size , without array#uniq
)