Guest User

Untitled

a guest
Feb 24th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. import itertools
  2.  
  3. def countArray(n,k,x):
  4. buildlis=[0]*(k+2)
  5. buildlis[0]=1
  6. buildlis[-1]=x
  7. t=1
  8. i=1
  9.  
  10. while(t<k+1):
  11. buildlis[i]=t%(k+1)
  12. t+=1; i+=1
  13.  
  14.  
  15.  
  16. #returns all possible combinations of numbers we need
  17. allperm=itertools.permutations(buildlis[1:-1], n-2)
  18. count=0;
  19. for perm in allperm:
  20. i=1; doub=False
  21. lis=list(perm)
  22. lis.insert(0,1)
  23. lis.insert(len(lis), x)
  24. #print(lis)
  25.  
  26. while(i<n):
  27. if lis[i-1]==lis[i]:
  28. doub=True
  29. break #efficiecny
  30. i+=1
  31. if doub==False:
  32. print(lis) #let's see the ones that pass the criteria set
  33. count+=1
  34.  
  35. return ("count array: ", count)
  36.  
  37. print(countArray(4,4,2))
Add Comment
Please, Sign In to add comment