Pulling all n-sized combinations from a list

M

mensanator

Magnus said:

I have, although I haven't tried it as I was able to get a GMPY
Windows binary from someone else. It may be that probstat
is not as complicated as GMPY. So if I get some free time,
I might try it, would be worth it in the long run. Thanks for
pointing it out.

Nevertheless, my "elusive" comment was certainly justified:

<quote>
Note also that the author of the document (Mike Fletcher)
has abandoned Windows as a development platform
(in some part because of the difficulties in getting a
working compiler to support the platform), and is thus
no longer actively maintaining this page. While some
people are still reporting successes, others are having
difficulties with building certain projects. The Toolkit
approach is, in short, a bit dicey as a platform for serious
development.
</quote>

I think it's safe to say most Windows users are NOT software
developers. If the professional software developers have trouble
figuring it out, what chance does an inexperienced end user have?

And for the record, I never implied that JD was obligated to provide
a Windows binary for his project. I was merely pointing out that
there are a lot of people not using his module because of the lack
of a Windows binary, so it really should come as no surprise that
people keep asking for solutions to combination/permutation
problems.

But it turns out he actually had one, which he graciously provided
in response to my observation. If I had kept my trap shut, I wouldn't
have it, would I? Squeaky wheels get grease. And, through my
blundering tests, he found a place where he should be raising an
exception, so he benefits also.

Luckily, not everyone has the "here's a nickel kid, get yourself a
better computer" attitude.
 
M

mensanator

Magnus said:
I completely agree, but you could put your "questions" in
a way that increases your chances of helpful replies...
http://www.catb.org/~esr/faqs/smart-questions.html

Ok, but I wasn't asking a question. I was answering the
question "why is nobody using my program?". Now, if _I_
had specifically asked how _I_ could use his program,
then the snide remarks about the availability of free
compilers on the Internet would have been somewhat
justified. I had already given up any hope of ever running
probstat, so I wasn't really looking for an answer since
I was already aware of what it would take for me to
compile it on Windows. It was just a lucky coincidence
that by mentioning Windows, JD replied that he had
a pyd available.

And, strange as it may seem, asking questions the
smart way is sometimes less effective than being a
smartass. Given a chance to say

idiot, you're wrong and here's why...

people will jump all over you. Take this thread,
for example. Of course, if they just call you an idiot
then you haven't gained anything.

But there's nobody like that on comp.lang.python, right?
 
S

Steve Holden

[...]
And, strange as it may seem, asking questions the
smart way is sometimes less effective than being a
smartass. Given a chance to say

idiot, you're wrong and here's why...

people will jump all over you. Take this thread,
for example. Of course, if they just call you an idiot
then you haven't gained anything.

But there's nobody like that on comp.lang.python, right?

Of course not, you idiot :)

regards
Steve
 
J

jess.austin

hi,

I'm not sure why this hasn't come up yet, but this seems to beg for
list comprehensions, if not generator expressions. All of the
following run in under 2 seconds on my old laptop:
alph = 'abcdefghijklmnopqrstuvwxyz'
len([''.join((a,b,c,d)) for a in alph for b in alph for c in alph for d in alph]) 456976
len([''.join((a,b,c,d)) for a in alph for b in alph for c in alph for d in alph
.... if (a>=b and b>=c and c>=d)])
23751
len([''.join((a,b,c,d)) for a in alph for b in alph for c in alph for d in alph
.... if (a!=b and b!=c and c!=d and d!=a and b!=d and a!=c)])
358800
len([''.join((a,b,c,d)) for a in alph for b in alph for c in alph for d in alph
.... if (a>b and b>c and c>d)])
14950

cheers,
Jess
 
M

mensanator

hi,

I'm not sure why this hasn't come up yet, but this seems to beg for
list comprehensions, if not generator expressions. All of the
following run in under 2 seconds on my old laptop:
alph = 'abcdefghijklmnopqrstuvwxyz'
len([''.join((a,b,c,d)) for a in alph for b in alph for c in alph for d in alph]) 456976
len([''.join((a,b,c,d)) for a in alph for b in alph for c in alph for d in alph
... if (a>=b and b>=c and c>=d)])
23751
len([''.join((a,b,c,d)) for a in alph for b in alph for c in alph for d in alph
... if (a!=b and b!=c and c!=d and d!=a and b!=d and a!=c)])
358800
len([''.join((a,b,c,d)) for a in alph for b in alph for c in alph for d in alph
... if (a>b and b>c and c>d)])
14950

Well, _I_ was using them in my original program to create the
conditionals, but not for the loops. So if we combine your idea
to use list comprehensions for the loop with my list comprehensions
of the conditionals, we aren't limited to asking for only 4-letter
words.

And it runs faster than my original.

Thanks.

def ooloop6(a, n, perm=True, repl=True):
if (not repl) and (n>len(a)): return
r0 = range(n)
r1 = r0[1:]
if perm and repl: # ok
v = ','.join(['c%s' % i for i in r0])
f = ' '.join(['for c%s in a' % i for i in r0])
e = ''.join(["p = [''.join((",v,")) ",f,"]"])
exec e
return p
if (not perm) and repl: # ok
v = ','.join(['c%s' % i for i in r0])
f = ' '.join(['for c%s in a' % i for i in r0])
i = ' and '.join(['(c%s>=c%s)' % (j,j-1) for j in r1])
e = ''.join(["p = [''.join((",v,")) ",f," if ",i,"]"])
exec e
return p
if perm and (not repl): # ok
v = ','.join(['c%s' % i for i in r0])
f = ' '.join(['for c%s in a' % i for i in r0])
i = ' and '.join([' and '.join(['(c%s!=c%s)' % (j,k) for k in
range(j)]) for j in r1])
e = ''.join(["p = [''.join((",v,")) ",f," if ",i,"]"])
exec e
return p
if (not perm) and (not repl): # ok
v = ','.join(['c%s' % i for i in r0])
f = ' '.join(['for c%s in a' % i for i in r0])
i = ' and '.join(['(c%s>c%s)' % (j,j-1) for j in r1])
e = ''.join(["p = [''.join((",v,")) ",f," if ",i,"]"])
exec e
return p

p = ooloop6('abcdefghijklmnopqrstuvwxyz',4,True,True)
print
print len(p)
p = ooloop6('abcdefghijklmnopqrstuvwxyz',4,False,True)
print
print len(p)
p = ooloop6('abcdefghijklmnopqrstuvwxyz',4,True,False)
print
print len(p)
p = ooloop6('abcdefghijklmnopqrstuvwxyz',4,False,False)
print
print len(p)

"""
456976

23751

358800

14950
"""
 

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,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top