Reputation: 123
I have this following code:
The code is simply stuck when I give input n=6 ,(in jupyter)I don't know what to do please help.
n=int(input("Enter a number to experiment:"))
l=[]
w=[]
nw=[]
c=0
for i in range(2*n):
if(i<n):
l.append(0)
else:
l.append(1)
import itertools as it
permset=set([i for i in it.permutations(l)])
for x in permset:
w.append(x)
print (w)
for j in range(len(w)):
c=1
for y in range(2*n-1):
if(w[j][y]==w[j][y+1]):
c=c
else:
c=c+1
nw.append(c)
print(nw)
from collections import Counter
Counter(nw)
Upvotes: 2
Views: 110
Reputation: 2220
The problem is the call to itertools.permutations
. It is computing every possible ordering of the array built previously, which for N=6 is:
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
I believe this will return (12!)=479001600 items, which your code is then turning into a set to remove duplicates. It never gets that far of course, but if it did, I believe the 479001600 items would be turned into a set of just 924 items.
Not sure what your goal is but maybe what you need for this part is a way to generate that set of 924 items without building the larger list, which has many many duplicates. As I understand it, that part of the code is building the set of arrays of size (2*n) where exactly half of the elements are 0 and half are 1.
Upvotes: 1