sai krishna wrote:

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

Welcome.

I wanted to print out all the combinations of a given word.

More precisely you want to print all the combination of the letters in 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?

For starters see http://en.wikipedia.org/wiki/Combination and http://en.wikipedia.org/wiki/Combinadic. At the bottom of the latter is a link
I have not examined that but it seems to be what you want.

-- 
Bob Gailer
919-636-4239 Chapel Hill, NC


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to