[Edu-sig] Pythonic Math must include...

kirby urner kirby.urner at gmail.com
Tue May 26 02:41:02 CEST 2009


On Sun, May 24, 2009 at 8:01 PM, Daniel Ajoy <da.ajoy at gmail.com> wrote:

> I've some other candidates that I have included in my Logo Functional
> Extensions (LogoFE).
>
> * A function that receives a math function and plots it.
> * A function that receives a math equation and an interval and finds a root
> in that interval
> * A function that receives a math function and an interval and calculates
> the area under that curve
> * The Ruffini algorithm
> * A function that receives a math function and a X coordinate and finds the
> derivative at that point. Or returns the derivative function if you like.
>
> * Set arithmetic. Including set product. This, together with "join" and
> list comprehensions is all you need instead of SQL
>
> * Functions that produce permutations and combinations of a tuple.
>
>
> Daniel
>
>

All excellent.

One reflex is to "jump to" and start contributing examples, ala PyWhip.

Another reflex is just to agree with the other pros on this list, that we've
got a great list, and then suggest anyone working to cut teeth, in
mathematics not just computer languages, try their hand at at least a few of
these, if not all of them.

On the subject of permutations, I was just happening to share Python with a
noob recently, not that unusual, and wrote the following, which we all know
is "not efficient" and yet conceptually fun, and we cut ourselves slack
here, like what's a few microseconds or even seconds.

If the thing blows up, we know how to kill it.

So what I'm doing in the snippet below is "stuffing a stocking" with all
permutations of 'A', 'B', 'C', 'D' or "four pool balls" (imagine using all
15: 1 + 2 + 3 + 4 + 5:
http://images.google.com/images?hl=en&q=pool+balls+triangle

using the technique of repeated shuffling and using the "set" data structure
(one among many in this philosophy) to screen out the dupes.

We already know the expected number (not cheating, no), so use this code
merely to get the list, which I should bother to sort in the end...  is this
the "best way to do it"?  Might be, if you're wanting to work with the
random module for awhile, do some of those Pascal's Triangle things with the
bell curve and so on.  I left in the error about "hashable" as we're
definitely in MD5 territory etc. i.e. unique mapping indexes, auto-generated
from the content, small chance of collision).

No need to turn up one's nose at "brute force" at every turn, as this new
meaning, involving delicate silicon, is hardly that brutish (IMO).

"""
IDLE 1.2.1
>>> from random import shuffle
>>> emptyset = set()
>>> thecombo = ['A','B','C','D']
>>> # number of sorts:  4*3*2*1 == 24
>>> bagosorts = set()
>>> shuffle(thecombo)
>>> thecombo
['B', 'C', 'D', 'A']
>>> len(bagosorts)
0
>>> while len(bagosorts) < 24:
       bagosorts.add(thecombo)
       shuffle(thecombo)



Traceback (most recent call last):
 File "<pyshell#11>", line 2, in <module>
   bagosorts.add(thecombo)
TypeError: list objects are unhashable
>>> # shit!
>>> while len(bagosorts) < 24:
       bagosorts.add(tuple(thecombo))
       shuffle(thecombo)


>>> len(bagosorts)
24
>>> print bagosorts
set([('B', 'A', 'D', 'C'), ('A', 'D', 'B', 'C'), ('A', 'C', 'D', 'B'),
('B', 'C', 'D', 'A'), ('B', 'D', 'C', 'A'), ('C', 'D', 'B', 'A'),
('C', 'A', 'B', 'D'), ('D', 'B', 'A', 'C'), ('B', 'A', 'C', 'D'),
('A', 'D', 'C', 'B'), ('D', 'C', 'A', 'B'), ('C', 'A', 'D', 'B'),
('C', 'B', 'A', 'D'), ('D', 'A', 'B', 'C'), ('A', 'B', 'D', 'C'),
('D', 'C', 'B', 'A'), ('C', 'B', 'D', 'A'), ('A', 'B', 'C', 'D'),
('B', 'D', 'A', 'C'), ('B', 'C', 'A', 'D'), ('D', 'B', 'C', 'A'),
('D', 'A', 'C', 'B'), ('A', 'C', 'B', 'D'), ('C', 'D', 'A', 'B')])


"""


Kirby
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20090525/e5e1b2fb/attachment.htm>


More information about the Edu-sig mailing list