आपको अधिक पृष्ठभूमि प्राप्त करने के लिए इस तरह के क्रमपरिवर्तन (अन्य चीजों के साथ) my blog post पढ़ना चाहिए - और वहां कुछ लिंक का पालन करें।
यहाँ है के रूप में अनुरोध मेरी कोषगत क्रम-परिवर्तन का एक संस्करण Steinhaus-जॉनसन-ट्रोटर क्रमचय जनरेटर करता है की पीढ़ी अनुक्रम के बाद जमाने जनरेटर:
def l_perm3(items):
'Generator yielding Lexicographic permutations of a list of items'
if not items:
yield [[]]
else:
dir = 1
new_items = []
this = [items.pop()]
for item in l_perm(items):
lenitem = len(item)
try:
# Never insert 'this' above any other 'this' in the item
maxinsert = item.index(this[0])
except ValueError:
maxinsert = lenitem
if dir == 1:
# step down
for new_item in [item[:i] + this + item[i:]
for i in range(lenitem, -1, -1)
if i <= maxinsert]:
yield new_item
else:
# step up
for new_item in [item[:i] + this + item[i:]
for i in range(lenitem + 1)
if i <= maxinsert]:
yield new_item
dir *= -1
from math import factorial
def l_perm_length(items):
'''\
Returns the len of sequence of lexicographic perms of items.
Each item of items must itself be hashable'''
counts = [items.count(item) for item in set(items)]
ans = factorial(len(items))
for c in counts:
ans /= factorial(c)
return ans
if __name__ == '__main__':
n = [1, 1, 1, 0, 0]
print '\nLexicograpic Permutations of %i items: %r' % (len(n), n)
for i, x in enumerate(l_perm3(n[:])):
print('%3i %r' % (i, x))
assert i+1 == l_perm_length(n), 'Generated number of permutations is wrong'
उपरोक्त कार्यक्रम से उत्पादन उदाहरण के लिए निम्नलिखित है:
Lexicograpic Permutations of 5 items: [1, 1, 1, 0, 0]
0 [1, 1, 1, 0, 0]
1 [1, 1, 0, 1, 0]
2 [1, 0, 1, 1, 0]
3 [0, 1, 1, 1, 0]
4 [0, 1, 1, 0, 1]
5 [1, 0, 1, 0, 1]
6 [1, 1, 0, 0, 1]
7 [1, 0, 0, 1, 1]
8 [0, 1, 0, 1, 1]
9 [0, 0, 1, 1, 1]
यदि यह होमवर्क है, तो तदनुसार टैग करें। और एन \ select k द्वारा आपका क्या मतलब है? – Rndm
@shg नहीं, यह होमवर्क नहीं है। ;) और n \ select k से मेरा मतलब है के-संयोजन (बिनोमियल गुणांक) की संख्या। – ushik