interesting exercise

James Stroud jstroud at mbi.ucla.edu
Tue May 8 00:11:53 EDT 2007


Michael Tobis wrote:
> I want a list of all ordered permutations of a given length of a set
> of tokens. Each token is a single character, and for convenience, they
> are passed as a string in ascending ASCII order.
> 
> For example
> 
> permute("abc",2)
> 
> should return ["aa","ab","ac","ba","bb","bc","ca","cb","cc"]
> 
> and permute("13579",3) should return a list of 125 elements
> ["111","113", ... ,"997","999"]
> 
> permute("axc",N) or permute("2446",N) should raise ValueError as the
> alphabet is not strictly sorted.
> 
> I have a reasonably elegant solution but it's a bit verbose (a couple
> dozen lines which I'll post later if there is interest). Is there some
> clever Pythonism I didn't spot?
> 
> thanks
> mt
> 

1. You oughtta go ahead and post it.
2. Have you checked out xpermutations?

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/190465

James



More information about the Python-list mailing list