yet another noob question

Simon Forman rogue_pedro at yahoo.com
Sun Aug 13 15:07:49 EDT 2006


Stargaming wrote:
> Stargaming schrieb:
> > mike_wilson1333 schrieb:
> >
> >> 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.
> >>
> >>
> >>       Thanks,
> >>
> >>       Mike
> >>
> >
> > Generally, it is range(11111, 55555)
> >
> > Sincerely,
> > Stargaming
>
> Whoops, I'm sorry. I think I was a little bit too enthusiastic and "wow
> look 'print range' is fun!". You could do a list comprehension over the
> range thingy.


def stoopid_way(start, end):
    for n in xrange(start, end + 1):

        # Make a string.
        s = '%d' % n

        # Exclude 0's.
        if '0' in s: continue

        # Exclude 6-9's.
        try:
            int(s, 6)
        except ValueError:
            continue

        yield s

Use it like so:

# Get all the strings as a list..
data = list(stoopid_way(11111, 55555))

# ..or print the strings one-by-one..
for s in stoopid_way(11111, 55555):
    print s

# ..or print one big string, joined by newlines.
print '\n'.join(stoopid_way(11111, 55555))


I originally meant this as a joke and was going to say not to use it.
But on my old, slow computer it only takes about a second or two.  If
that's fast enough for you then it won't do any harm to use it.  (Just
don't mention my name ;-)  )

Peace,
~Simon




More information about the Python-list mailing list