Anagram

Eric Brunel eric.brunel at pragmadev.com
Wed Jan 23 10:25:54 EST 2002


Hi,

> Friend of mine who used to work with lisp (and is a bit interested in
> Python) asked me how compact I could write a program to evaluate the
> number of possible combinations a set of characters (string) can be
> written in - handling two identical characters as different
> characters.

Er, correct me if I'm wrong, but remembering my math classes, the shorter 
way is probably:

def fact(x): return (x <= 1 and 1) or (x * fact(x-1))
print fact(len(s))

since, if I remember well, the number of permutations of a set of x 
elements is x! (excuse the "(... and ...) or (...)" thing: I was in Lisp 
mode ;-)

BTW, I thought there was a built-in function returning x! somewhere, but I 
didn't find it... Was it a dream?
 - eric -




More information about the Python-list mailing list