Advertisement
here2share

# fast_permute_unique.py

Feb 4th, 2021
1,253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.42 KB | None | 0 0
  1. # fast_permute_unique.py
  2.  
  3. def permute_unique(nums):
  4.     perms = [[]]
  5.     for n in nums:
  6.         new_perm = []
  7.         for perm in perms:
  8.             for i in range(len(perm) + 1):
  9.                 new_perm.append(perm[:i] + [n] + perm[i:])
  10.                 # handle duplication
  11.                 if i < len(perm) and perm[i] == n: # skips a few
  12.                     break
  13.         perms = new_perm
  14.     return perms
  15.  
  16. zzz = permute_unique([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3])
  17.  
  18. for z in zzz:
  19.     print(z)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement