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

Thura has posted 1 posts at DZone. View Full User Profile

Python Script To Print Lexicographic Permutation

12.11.2008
| 5120 views |
  • submit to reddit
        #  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

he problem with calling a recursive function is that you often end up having to do callbacks if you are working with a large amount of data. teletext holidays - lanzarote weather - waterless cookware - calphalon nonstick

flash tring replied on Thu, 2009/04/23 - 7:07am

It is a very nice post to share in my Resume writings.I found that python script is much better than the other scripts.

fien terst replied on Wed, 2009/04/22 - 2:22am

I was facing this problem since from a week.......Thanks for providing the useful script...I really appreciate for this...... Acne treatments

Snippets Manager replied on Tue, 2009/04/21 - 2:41pm

The script was awesome, I Have been looking out for this. Thanks for sharing. Auto Hifi

sdadfam faomdm replied on Sat, 2009/04/18 - 11:38am

this is really awesome code mate. I have got this code at review center exactly 1days before. Anyhow thanks a lot for sharing. It is working successfully in my website car games. regards, dictonary jhonson

Snippets Manager replied on Fri, 2009/02/20 - 4:04pm

Snippets Manager replied on Wed, 2009/01/07 - 2:02am

Here we demonstrate the power of recursion! I give you two example loops for doing permutations, one of which is more readable and slower! The problem with calling a recursive function is that you often end up having to do callbacks if you are working with a large amount of data. the cool thing about recursion is that it can be super-fast and mind-bending! I just couldn't resist the pontential here! but in a giant program you'd probably want to do a in-function for loop to do permutations. boring as those are, they are more readable than any called recursive function. see if you can figure out how to make a for loop which does the same thing as these recursive functions! it is possible for all recursion! #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))