yet another noob question

mensanator at aol.com mensanator at aol.com
Sun Aug 13 19:29:25 EDT 2006


Sybren Stuvel wrote:
> mike_wilson1333 enlightened us with:
> > 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.
>
> Count from 0 to 44444 in a base-5 numbering, and add 11111 to every
> number.
>
> b5_11111 = int('11111', 5)
>
> for i in range(int('44444', 5)+1):
>     i += b5_11111
>     print i
>
> Only the print command needs to convert back to base-5. Does anyone
> know of an elegant way to do that?

Use the gmpy module which does base conversion in both directions.

>>> import gmpy
>>> b5_11111 = gmpy.mpz('11111',5)
>>> b5_11111
mpz(781)
>>> print gmpy.digits(b5_11111,5)
11111


>
> Sybren
> --
> The problem with the world is stupidity. Not saying there should be a
> capital punishment for stupidity, but why don't we just take the
> safety labels off of everything and let the problem solve itself?
>                                              Frank Zappa




More information about the Python-list mailing list