全排列的一个实现。
import math
def per( seq ):
count = len( seq )
resCount = math.factorial( count )
ress = [ ['']*count for i in range(resCount) ]
for i in range(count):
base = math.factorial( count-1-i )
for index in range(resCount):
newseq = [ x for x in seq if x not in ress[index] ]
ssi = (index // base) % len(newseq)
ress[index][i] = newseq[ssi]
return ress
def test():
ress = per( 'abcd' )
#ress = per( 'abcde' )
#ress = per('123456789')
for res in ress:
for x in res:
print( x, end='' )
print()
if __name__ == "__main__":
test()