yet another noob question

Paul Rubin http
Sun Aug 13 15:53:54 EDT 2006


"mike_wilson1333" <mwilson1333 at hotmail.com> writes:
> I would like to generate every unique combination of numbers 1-5 in a 5
> digit number and follow each combo with a newline.  So i'm looking at
> generating combinations such as: (12345) , (12235), (55554) and so on.
> What would be the best way to do this? So, basically i'm looking for a
> list of all combinations of 1-5 in a 5 digit unique number. Also, when
> I send the list to print there can't be any duplicates of the combos.

Basically you want to print a list of integers in 5-digit base-5
notation, except relabelling the digits.  There will be (5**5 - 1)
numbers in the list.  You want to print 0 as '11111', 1 as '11112',
etc.  Here's one way to get the representation.  It's sort of a
"functional" approach using recursion, but feels natural if you're
into that sort of thing:

    def base5x(k, ndigits):
        if ndigits == 1: return '%d'% (k+1)
        return base5x(k//5, ndigits-1) + base5x(k%5, 1)

Now you can use the function to print the list you wanted:

    for i in xrange(1234,1333):
       print base5x(i, 5)



More information about the Python-list mailing list