Anagram

David Lees deblNonospammyWhammy at raqia.com
Wed Jan 23 15:57:55 EST 2002


You should be a little careful using the recursive implementation of
factorial, because limited recursion depth will make it useless for
large values of 'n'

A better non-recursive function would be:

def fact(n):
    z=n*1L
    while (n>1):
        n=n-1
        z *= n
    return z

which has no problem doing 1200 factorial, while the recursive version
fails.

David Lees

Jason Orendorff wrote:
> 
> Nikolai Kirsebom wrote:
> > 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.
> 
> Python's strength is not "how compact" but "how clear".
> 
> The number you want is the factorial of the length of the string.
> 
> import sys
> 
> def f(n):
>     if n < 2:  return 1
>     return n * f(n-1)
> 
> for a in sys.argv[1:]:
>     print a, f(len(a))
> 
> ## Jason Orendorff    http://www.jorendorff.com/

-- 
debl



More information about the Python-list mailing list