combination function in python

mensanator at aol.com mensanator at aol.com
Mon Apr 16 21:42:55 EDT 2007


On Apr 16, 6:40 pm, Anton Vredegoor <anton.vredeg... at gmail.com> wrote:
> mensana... at aol.com wrote:
> > Isn't that what docstrings are for? Can't you leave
> > the function name noverk() and add something to the
> > effect of "this function calculates combinations"?
> > Then it would show up in searches, wouldn't it?
>
> Yes, a doc string would help finding it in searches, however since this
> thread now contains a collection of function names it will suffice.
>
> There is also the other consideration of being able to easily read code
> that others write. Functions that try to do the same thing having the
> same name would help.

Why not simply the same doc string? You can't expect everyone to
use the same function name.

> If we had some module in the standard distro that
> would contain comb and fac and maybe a few other functions people could
> start using the same name to point to the same thing etc.

You COULD add those to the standard distro. You COULD read the
newsgroups and get various Pythonic solutions. But you don't
HAVE to if you're looking just for a solution.

>
> >>>> def noverk(n,k):
> >>>> ? ? ?return reduce(lambda a,b: a*(n-b)/(b+1),xrange(k),1)
> >> This is a rather concise function which has the added advantage that it
> >> returns 0 when k>n.
>
> > import gmpy
> > print gmpy.comb(4,8)
>
> > ## 0
>
> There is more to it than that. If I see the way the function computes
> the values it gives me hints about how I could write a function that
> lists all combinations.

But you said returning 0 was an advantage, as if that's not
typical. I was merely pointing out that it is NOT an advantage
over gmpy. Giving you hints about further application is
a different advantage than what you listed.

>
> For example the fact that one can divide the product 'on the fly' -I
> mean without computing the totals above and below the division bar-
> tells me something about how to tackle the problem of generating the
> corresponding combinatorial structure. I *love* combinatorial
> structures. Little functions like this have more than once helped me to
> understand them better.

Fine, and gmpy doesn't give you the actual combinations, so I wrote
my own. But if there's a faster, better 3rd party library that
can do the same things mine does (permutations with replacement,
combinations with replacement, permutations without replacement and
combinations without replacement) then I'll drop mine like a live
grenade.

I wrote this because I needed to use it in a bigger application,
the details were not important, just the output. It's the bigger
applications details I'm concerned about.

>
> Math is not so much my way of describing things, I like executable
> pseudo code better. Sometimes I had to read math formulas and have
> become somewhat used to that notation -it has its uses too- but if
> things are written down in code I can rely on the *computer* to execute
> the code instead of my brain which unfortunately is not as reliable.
>
> These advantages are lost when one just imports some optimized code library.

And once I looked up the Extended Euclidean Algorithm, made a Python
implementaion of it and after I got it all working, I found it was
all unnecessary because there was a gmpy function already present
(which I had previously ignored because it didn't understand what
it meant) that did exactly what I want. Yes, I'm glad I went through
the trouble of learning the algorithm, but I simply threw away my
version.

You talk about losing the advantage of learning how to do this,
but is there any difference between your code and gmpy as far as
the OP is concerned? Is he going to actually learn anything from
your program or is he just going to use it? Is it going to matter
to the OP whether he uses a 3rd party module or some code (that he
doesn't understand) that he found on Usenet?

>
> > Perhaps you should use the name
>
> > comb_works_just_like_the_gmpy_version_only_slower()
>
> Or maybe 'comb_in_executable_pseudocode', but maybe some other
> implementations posted in this thread would be better suited for that.
>
> Anyway, I have the impression you just don't get the point of people
> posting various functions so that one can determine which algorithms
> work best or so that one can include each others ideas and create better
> functions that combine the best elements of all posted code. Cutting and
> pasting Python functions works a lot better in Usenet than importing C
> libraries.

"Works better" for learning the algorithms, doesn't "work better"
performance wise.

>
> If no one posts code -even if it's not completely working code- how are
> we to learn from each other?

By focusing on things that aren't available in 3rd party modules.

> The fact that someone else somewhere
> already saw the light and wrote the perfect function in optimized C or
> Fortran code should not stop us from 'reinventing the wheel' -as you
> call it- because one can get more proficient in wheel making in the
> process and every now and then it even leads to better wheels.

But there's no reason why anyone should use your re-invented wheel
in place of the old wheel.

>
> Don't even get me started about this archaic scheme of writing code only
> for 'work' instead of for gaining insight that you seem to promote.

I didn't mean 'work' in any kind of business sense (all my Python
code is strictly amateur mathematics). I meant when you have a task
to solve, learning how the tools are made is not important, it's
USING the tools. Sure, it's possible to have gained some insight
into using the tools by knowing how they're made, but as I said with
the EEA, you can keep the insight and throw the program away if
there's
a better alternative in a 3rd party module.

>
> > But when there *is* no documentation, that becomes a problem,
> > doesn't it?
>
> This thread is also documentation and I hope it will encourage people to
> post more code and share thoughts.
>
> > Unless you don't know how to write the functions you need
> > in which case you're better off relying on external
> > modules. Have you ever wondered why Python even *has*
> > a standard library? Why doesn't everyone just write
> > the functionality they need?
>
> The main virtue of Pythons standard library should be that it shows one
> how to do things and that it introduces a common naming scheme. That was
> what got me interested in python in the first place: Now I got to see
> how things work! No more dark secrets and proprietary code!

I think you would get an argument that that's its MAIN virtue.

>
> Sometimes it's better to sacrifice clarity for speed and write optimized
> lower level code but that doesn't mean there aren't large trade offs
> involved.

And sometimes it's not. Sometimes high-level clarity is better than
low-level clarity. I have no concern about HOW the modular inverse
function works, what I care about is how to use it to solve a linear
congruence.

>
> In the future if computers will become faster it would be wise to drop
> the optimized code libraries again and reintroduce the Python versions
> of the same because by then the Python versions would be fast enough and
> it would also result in a smaller code base (but a more functional and
> flexible and readable one).

You're wrong here. Python and the computers it runs on will NEVER be
fast enough, even if Python was compiled and executing native code.
When things get faster, the problems get bigger. All the improvement
does is push back the threshhold of intractability, it can never make
it go away.

>
> What are you doing on this planet anyway if it's not trying to
> understand things?

I'm trying to understand what *I* want to understand and ignore
what I don't NEED to understand.

>
> >> Since I'm also interested in the
> >> functions themselves -in this case- I'd rather have a
> >> few lines of code in my source than importing an
> >> optimized code library.
>
> > Well, some people prefer optimized libraries because
> > they have real work to do, not just acedemic excercizes.
>
> Real work is just an excuse for having a larger slice of the cake than
> other people. Other people can be just as or even more essential in the
> whole process of developing code than those who get paid.

As I said earlier, nothing to do with getting paid. The problems
I work on are bigger than algorithms for calculating combination
counts, so my focus is on them and if I can employ a 3rd party
module, then that's more effort I can devote to the problem.

>
> A reason against using third party libraries in general is not wanting
> to include too many external dependencies that would force the user to
> download all kinds of things from different places which could change
> their urls or web interfaces. It would also introduce more points where
> version conflicts or license conflicts could occur.

Strange, you think posting code on Usenet to share with others is
good,
yet you disparage those who formalize that sharing as a module.

>
> > So it doesn't matter whether anyone can find noverk(),
> > does it?
>
> It does matter a lot in the long run if people can compare their code to
> the code other people write. If still other people profit from these
> public exchanges and then adopt a condescending attitude towards the
> grass roots processes -either because of misguided adoration for the
> best programmers or because they think themselves to be the best or
> maybe because of some archaic work related value system- that would
> dampen the enthusiasm a bit.

I think you're wrong here. I didn't say people shouldn't be posting
these solutions, I said anyone who knows better won't be looking for
them.
Trust me, I was MUCH more enthusiastic about solving my linear
congruence
problem (which I had been toying with for about a year - calendar
time)
than I was over knowing how to do an Extended Euclidean Algorithm.

>
> >> You could take it up with the gmpy author and
> >> induce him to get gmpy included in the standard distro if you are so
> >> inclined.
>
> > Alex Martelli knows more about that subject than I and
> > it would be pointless for me to bug him about it.
>
> Perhaps even your kind of misguided criticism would be better than such
> a fatalistic attitude.

Fatalistic? I knew there were reasons, I just didn't care to make
any effort to remember them and saw no reason to look them up.

>
> A.





More information about the Python-list mailing list