[Edu-sig] re: Terminology question: just operator overriding?

Kirby Urner urnerk@qwest.net
Mon, 30 Jun 2003 22:07:26 -0700


At 06:06 PM 6/30/2003 -0700, Kirby Urner wrote:

>The pattern is like this:
>
>-------- Module -------------
>
>   def compose(map1, map2):
>       ...
>       return map3
>
>   class Permutation(object):
>       ...
>       def __mul__(self):

sorry, goofed:

        def __mul__(self, other):

>          return Permutation(compose(self.amap, other.amap))
>
>------------------------------

  >>> p1 = P(simplecode(letters))  # random permuation using A-Z plus space
  >>> p2 = P(simplecode(letters))  # ditto
  >>> p1
  [['A', 'W', 'Z', 'R', 'N', 'G'], ['C', 'Q', 'S', 'X', ' ', 'B', 'M'],
  ['E', 'V', 'L'], ['D', 'K', 'J', 'O', 'Y', 'P', 'H', 'T', 'U', 'F', 'I']]

Using cyclic notation e.g. A goes to W, W to R and so on.

  >>> p2
  [['A', 'Q', 'T', 'Y', 'P', 'S', 'D', 'K', 'I', 'M', 'U', 'O'], ['C'],
  ['B'], ['E', 'N', 'G', 'V', 'H', ' ', 'W'], ['F', 'J', 'Z'],
  ['L', 'R', 'X']]

Here W goes to E (wraps around at the end of the cycle).

  >>> p1 * p2  # operator overloading
  [['A', 'E', 'H', 'Y', 'S', 'L', 'N', 'V', 'R', 'G', 'Q', 'D', 'I',
  'K', 'Z', 'X', 'W', 'F', 'M', 'C', 'T', 'O', 'P', ' ', 'B', 'U',
  'J']]

So the product takes A straight to E.  Interesting that the resulting
permutation has the one big cycle.  I didn't plan that.

  >>> encode("THIS IS A MESSAGE ENCODED USING A PERMUTATION",p1.map)
  'UTDXBDXBWBCVXXWAVBVGQYKVKBFXDGABWBHVNCFUWUDYG'

  >>> decode('UTDXBDXBWBCVXXWAVBVGQYKVKBFXDGABWBHVNCFUWUDYG',p1.map)
  'THIS IS A MESSAGE ENCODED USING A PERMUTATION'

Kirby