[Tutor] Obtaining various combinations of a given word
Brendan
brendan.rankin at gmail.com
Wed Jul 30 01:57:47 CEST 2008
sai krishna <sparkignited <at> gmail.com> writes:
>
>
> 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 nelif
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!!!
Found the following link while googleing. It looks pretty nice and makes good
use of Python's generators.
http://www.daniweb.com/code/snippet553.html
Cheers,
- Brendan
More information about the Tutor
mailing list