analitics

Pages

Saturday, August 8, 2009

Permutation of chars with python

It is a simple example of using python language to permutation of chars.
It uses a list generator and "yield":

#!/usr/bin/env python
def permut(lst):
remove = lambda lst0, index: lst0[:index] + lst0[index+1:]
if lst:
for index, x in enumerate(lst):
for y in permut(remove(lst, index)):
yield (x,)+y
print y

else: yield ()
lst='catalin'
for p in permut(['c','a','t','a','l','i','n']):
print p

Example can be modified so output will be a file.