Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # given a list of categories giving the number of multi-combinations of each type,
- # e.g. [1, 3, 2] would correspond to strings like 011122
- # generate each permutation in order
- # if two categories have the same quantity, only generates permutations in which
- # the first element of the lower-numbered category comes before the first element of the higher-numbered category
- def combEnum2(categories):
- n = sum(categories)
- # start with minimum permutation
- result = []
- for j,k in enumerate(categories):
- result.extend([j]*k)
- n = len(result)
- teamsUsed = list(categories)
- while True:
- print result
- x = n-2
- teamsUsed[result[x+1]] -= 1
- teamsUsed[result[x]] -= 1
- while True:
- if result[x] < result[x+1]:
- firstX = teamsUsed[result[x]] == 0
- swap = 9999
- swapIndex = -1
- for y in xrange(x+1,n):
- if result[y] < swap and result[y] > result[x] \
- and (not firstX or categories[result[y]] != categories[result[x]]):
- if teamsUsed[result[y]] == 0:
- ok = True
- for t in xrange(result[y]):
- if teamsUsed[t] == 0 and categories[result[y]] == categories[t]:
- ok = False
- if ok:
- swap = result[y]
- swapIndex = y
- if swapIndex > -1:
- teamsUsed[swap] += 1
- result[x] = swap
- z = 0
- for y in xrange(x+1, n):
- while teamsUsed[z] == categories[z]:
- z += 1
- result[y] = z
- teamsUsed[z] += 1
- break
- x-=1
- if x < 0:
- break
- teamsUsed[result[x]] -= 1
- if x < 0:
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement