yet another noob question

M

mike_wilson1333

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
 
S

Stargaming

mike_wilson1333 said:
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
--
 
S

Simon Forman

mike_wilson1333 said:
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

Hi Mike, this is one of those "Somebody must have had this problem
before me" kind of situations. Google on "python combination" and you
should be well rewarded. :)

Peace,
~Simon
 
R

Rene Pijlman

mike_wilson1333:
I would like to generate every unique combination of numbers 1-5 in
a 5 digit number [...] What would be the best way to do this?

Ask the Java newsgroup to design a clever algorithm and post it here for
pythonification. Ask for the pseudocode, not the Java code.
 
S

Stargaming

Stargaming said:
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.
 
S

Simon Forman

Stargaming said:
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
 
A

Alex Martelli

mike_wilson1333 said:
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.

[[warning: haven't tried out any of the following code, just typed it
into the newsreader, so there may be typos or whatnot, but, I'm focusing
on communicating the general ideas anyway:)]]


A working but boilerplatey solution is to nest 5 loops...:

r = range(1, 6)
for i0 in r:
for i1 in r:
for i2 in r:
for i3 in r:
for i4 in r:
print '(%d%d%d%d%d)' % (i0,i1,i2,i3,i4)

While this does work, it's inelegant -- "flat is better than nested",
and all that. Hard-coding the exact number of nested loops, etc, is
definitely not ideal.

So here's an alternative:

def allcomb(seq=range(1, 6), reps=5):
counter = reps*[0]
format = '(' + '%s'*reps + ')'
maxcount = len(seq)-1
while True:
print format % tuple(seq for i in counter)
i = 0
while i<reps and counter==maxcount:
counter = 0
i += 1
if i >= reps: return
counter += 1

You can have many variations on this theme, but basically what they're
doing is "counting in base N" (where N is len(seq)) -- variations around
that are mere stylistic issues. For example, the inner loop (after the
print statement) could become:
for i in range(reps):
if counter < maxcount:
counter += 1
break
else:
counter = 0
else:
return
or, you could loop with "for i, c in enumerate(counter):", etc, etc --
but these variations are all implementing the same algorithm. One
algorithms that's definitely different enough to distinguish is to use
recursion -- but it won't offer much advantage over this one.


Alex
 
S

Sybren Stuvel

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?

Sybren
 
B

bearophileHUGS

Simon Forman:
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.

Another possibility, still using a filter:

nodig = set("06789")
r = [i for i in xrange(11111, 55555+1) if not (set(`i`) & nodig)]

Bye,
bearophile
 
P

Paul Rubin

mike_wilson1333 said:
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)
 
P

Paul Rubin

Paul Rubin said:
Now you can use the function to print the list you wanted:

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


Whoops, pasted the wrong thing from my test window. Sorry. That
was supposed to say:

for i in xrange(5**5):
print base5x(i, 5)
 
M

mensanator

Sybren said:
mike_wilson1333 enlightened us with:

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.
 
J

Jason Nordwick

Or without filter:

from operator import add
def pr(x): print x
def cross(x,y): return reduce(add, [[a+b for b in y] for a in x])
x=map(pr, reduce(cross, [map(str,range(1,6))]*5))
 
J

Jason Nordwick

better (sorry, still learning Python):

def cross(x,y): return [a+b for a in x for y in b]

Jason said:
Or without filter:

from operator import add
def pr(x): print x
def cross(x,y): return reduce(add, [[a+b for b in y] for a in x])
x=map(pr, reduce(cross, [map(str,range(1,6))]*5))



mike_wilson1333 said:
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
 
S

Stargaming

Jason said:
Or without filter:

from operator import add
def pr(x): print x
def cross(x,y): return reduce(add, [[a+b for b in y] for a in x])
x=map(pr, reduce(cross, [map(str,range(1,6))]*5))
[...]

reduce(add, list) is the same as sum(list) and is only half as fast as sum:add").repeat(200, 1000))/200
0.10820861031220445
Also note that reduce will be removed in Python 3000.
 
G

Gerard Flanagan

mike_wilson1333 said:
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

Hi Mike

mod5 = ['1','2','3','4','5']

X = [ ''.join([a,b,c,d,e])
for a in mod5
for b in mod5
for c in mod5
for d in mod5
for e in mod5 ]

assert len(X) == 5**5
assert len(set(X)) == 5**5 #no duplicates


Gerard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top