DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Python Script To Print Lexicographic Permutation
# This is just a small python snippet to lexicographic print permutation from 0 to 9 .. like
# 0 1 2 3 4 5 6 7 8 9
# 0 1 2 3 4 5 6 7 9 8
# 0 1 2 3 4 5 6 8 7 9
# 0 1 2 3 4 5 6 8 9 7
# 0 1 2 3 4 5 6 9 7 8
# 0 1 2 3 4 5 6 9 8 7
# 0 1 2 3 4 5 7 6 8 9
# I am just a newbie learning programming with python, so excuse my bad coding ;)
import sys
count = 0
for a in range(0,10):
table = [False,False,False,False,False,False,False,False,False,False]
table[a] = True
for b in range(0,10):
if not table[b]:
table[b] = True
for c in range(0,10):
if not table[c]:
table[c] = True
for d in range(0,10):
if not table[d]:
table[d] = True
for e in range(0,10):
if not table[e]:
table[e] = True
for f in range(0,10):
if not table[f]:
table[f] = True
for g in range(0,10):
if not table[g]:
table[g] = True
for h in range(0,10):
if not table[h]:
table[h] = True
for i in range(0,10):
if not table[i]:
table[i] = True
for j in range(0,10):
if not table[j]:
print a,b,c,d,e,f,g,h,i,j
count = count + 1
table[j] = False
table[i] = False
table[h] = False
table[g] = False
table[f] = False
table[e] = False
table[d] = False
table[c] = False
table[b] = False






Comments
bela dona replied on Mon, 2009/05/18 - 8:41am
flash tring replied on Thu, 2009/04/23 - 7:07am
fien terst replied on Wed, 2009/04/22 - 2:22am
Snippets Manager replied on Tue, 2009/04/21 - 2:41pm
sdadfam faomdm replied on Sat, 2009/04/18 - 11:38am
Snippets Manager replied on Fri, 2009/02/20 - 4:04pm
Snippets Manager replied on Wed, 2009/01/07 - 2:02am
#dummy functions which are good examples of callbacks! def printer(x): print(x) return True def returner(x): return(x) # this one can do want you want and is very readable, but still recursive! def permute_readable_version(table=range(10), permutation=[], num_items=10, callback=printer): if len(permutation) == num_items or len(table)==0: return [callback(permutation)] permutations=[] for i in range(len(table)): permutations+=permute_readable(table[:i]+table[i+1:], permutation+[table[i]], num_items, callback) return permutations # this one can do the same thing and is hard to read but even faster! def permute(table=range(10), permutation=[], num_items=10, callback=printer): if len(permutation) == num_items or len(table)==0: return [callback(permutation)] return sum([permute(table[:i]+table[i+1:], permutation+[table[i]], num_items, callback) for i in range(len(table))],[]) # Try uncommenting the below line! #results=permute() # I set all the default values so it'll do exactly what you want! # If you use a dummy return function for every permutation, # you get back a list of all the permutations. # Without using messy messy global variables! # But careful! Huge lists of thousands of permutations can crash you # if they're all in memory at once! That's what callbacks are for! couples_permutation= permute(['joe six-pack', 'jesus', 'margaret thatcher', 'sarah palin', 'george bush', 'vishnu'], num_items=2, callback=returner) couples_combinatorial=[] for couple in couples_permutation: if sorted(couple) not in couples_combinatorial: couples_combinatorial.append(sorted(couple)) print("\n Who do you think would make a good couple?\n") for couple in couples_combinatorial: print(' %s with %s?' % (couple[0],couple[1])) print("\n There are %s possible couples listed.\n" % len(couples_combinatorial))