simple recursion help

Terry Reedy tjreedy at udel.edu
Sun Oct 24 17:39:00 EDT 2004


"drs" <drs at remove-to-send-mail-ecpsoftware.com> wrote in message 
news:VUyed.320061$bp1.273357 at twister.nyroc.rr.com...
> again.  I am looking for a list of all unique strings of length x whose
> elements are from the set a, b, and c.
>
> So, for example, in this case it would be
>
> aaa, aab, aac, aba, abb, abc, aca, acb, acc, ... cca, ccb, ccc

This is equivalent to finding all n digit base k numbers, where k is the 
size of the set and where numbers are padded on the left with 0s.  You 
example above is equivalent to

000, 001, 002, 010, 011, 012, 020, 021, 022, ... 330, 331, 333

There are lots of related algorithms for this set/sequence of N=n**k 
formatted numbers.  Which you really want depends on questions like:

Do you *really*need character strings or would range(N) or xrange(N) 
actually do as well?  Since the latter is obviously trivial, think about 
whether the application can be changed a bit to use numbers instead of 
string representations thereof.

Do you need to use an arbitrary set of characters or would a standard set 
'0', '1', ... do as well?

Do you you need the N=n**k strings all at once in an explicit list or set 
or will a generator producing them one at a time do?

Do you need them ordered, and if so, in any particular order, whether in a 
list or from a generator?

Terry J. Reedy






More information about the Python-list mailing list