Anagram

Nikolai Kirsebom nikolai.kirsebom at siemens.no
Wed Jan 23 07:36:25 EST 2002


Just for fun !!

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.

Example:
"ab" --> yields 2 ("ab", "ba")
"abc" --> yields 6
"aa" --> yields 2

My Python answer shown below.   Better solutions ?  Does anyone (here)
know how this could be done in Perl ?
-------------------
import sys

def f(s,i):
    if len(s) == 2 or i == 0: return min(i*len(s),2)
    return f(s[1:], len(s)-1) + f((s[1:] + s[0]), i - 1)

if len(sys.argv) < 2:
    print "Usage: python Script2.py words...."
    sys.exit(0)
for a in sys.argv[1:]:
    print a, f(a, len(a))

----------------





More information about the Python-list mailing list