[Edu-sig] Pythonic eToyz

kirby urner kirby.urner at gmail.com
Fri Mar 7 00:33:26 CET 2008


So I'm appending some code I'm kicking around in anticipation
of Pycon, call it "ambient Python for airports" (hello Brian Eno).

Given a specific sequence of math topics mostly motivates
our pathway through Python (starting with using it as a
calculator), we're likely:

(a) to encounter generators early (e.g. for Fibonacci numbers),
then

(b) very simple classes, with emphasis on operator overloading.

while of course

(c) ordinary functions, like "def f(x): return x**2" will always be
a part of it, along with those plotting libraries.

I.E. this isn't a CS course where you do procedural programming
for a year, before classes even get mentioned (such a methodic
and detailed approach will be left to a college setting -- this is
college prep).

In a first pass, we provide the code below as scaffolding, explaining
as we go, *not* requiring students to write it all from scratch
(though some might -- plus there's room for improvement (see
comments)).

When learning a language (human or computer), you want
to eyeball existing code, get used to the look in feel, e.g. in
this case Python's trademark __rib__ syntax.

So below is a good example of a class we'd encounter early
in our high school math learning career.

All it does is implement the four operations, plus the
ordering operators (< , >, ==), among integers modulo M,
with the modulus set at the class level.

We want students to have interactive experiences like:

>>> from chicago2 import M
>>> M.modulus
7
>>> m2 = M(2)
>>> m4 = M(4)
>>> m2 * m4
m1
>>> m2 / m4
m4
>>> m4 ** 2
m2
>>> [m2 ** i for i in range(10)]
[m1, m2, m4, m1, m2, m4, m1, m2, m4, m1]

Note we don't get a nice division operation for every modulus,
as there's not always a unique multiplicative inverse -- part of
what we're learning about (group, ring and field -- a first pass,
non-exhaustive).

We're putting more emphasis on modulo arithmetic, as well as
concepts of gcd, coprime, because we're aiming to encompass
RSA the public key algorithm by senior year high school, ala
Sarah Flannery's 'In Code' (a bestseller).**

Kirby

Primary reference:

A. Bogomolny, Modular Arithmetic from Interactive Mathematics
Miscellany and Puzzles
http://www.cut-the-knot.org/blue/Modulo.shtml, Accessed 06 March 2008

Secondary reference:
Urner, K. Vegetable Group Soup
http://urnerk.webfactional.com/ocn/flash/group.html
(apologies for the annoying machine noise)

** http://en.wikipedia.org/wiki/Sarah_Flannery

#=== test code ===

class M:

    modulus = 7

    def __init__(self, val):
        self.val = val % M.modulus
        # looking for mult. inverse (primitive brute force,
        # could use extended Euclid instead)
        for i in range(1, M.modulus):
            if (self.val * i) % M.modulus == 1:
                self.recip = i
                break

    def __repr__(self):
        return "%s mod %s" % (self.val, M.modulus)

    # note:  all of these return M-type objects

    def __add__(self, other):
        return M(self.val + other.val)

    def __sub__(self, other):
        return self + (-other)

    def __mul__(self, other):
        return M(self.val * other.val)

    def __truediv__(self, other):
        return self * other**(-1)

    def __neg__(self):
        return M(-self.val)

    def __pow__(self, e):
        # note: m ** (-e) == (m ** (-1)) ** e
        if e < 0:
            base = self.recip
        else:
            base = self.val
        return M(pow(base, abs(e), self.modulus))

    #======= ordering operators

    def __lt__(self, other):
        return self.val < other.val

    def __gt__(self, other):
        return self.val > other.val

    def __eq__(self, other):
        return self.val == other.val


More information about the Edu-sig mailing list