[Tutor] Obtaining various combinations of a given word

sai krishna sparkignited at gmail.com
Tue Jul 29 23:50:38 CEST 2008


Hi,everyone.
My name is Sai krishna, and I'm new to Python as well as Programming.

I wanted to print out all the combinations of a given word.
I am doing it this way:

n='cat'
def arrange(n):
if len(n)==1: #length of word is 1
    print n
elif len(n)==2: # length of word is 2
    print n[0]+n[1]
    print n[1]+n[0]
elif len(n)==3:
    print n[0]+n[1]+n[2]
    print n[0]+n[2]+n[1]
    print n[1]+n[0]+n[2]
    print n[1]+n[2]+n[0]
    print n[2]+n[0]+n[1]
    print n[2]+n[1]+n[0]

This process is quite lengthy, and I have managed to do this for word
containing 5 letters,i,e.,120 combinations
Is there a better way?
Please help.

-- 
cheers!!!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080730/390e2773/attachment.htm>


More information about the Tutor mailing list