[Tutor] Simple ROT-13 Script

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Tue Feb 3 02:31:08 EST 2004



On Mon, 2 Feb 2004, Matthew Hoffman wrote:

> I just wrote a quick one-off script and I would like your opinions on
> any Python-isms. It's just a ROT-13 script (I got a Neal Stephenson book
> for xmas). I would just like to entertain your thoughts about what I
> could have done better, etc. Cheers! ~Matt

> encrypt = lambda s: chr(ord(s) + 13)
> decrypt = lambda s: chr(ord(s) - 13)
>
> s = 'guido'
> r = map(encrypt, s)
> t = map(decrypt, r)
>
> >>>r
> ['t', '\x82', 'v', 'q', '|']
> >>>t
> ['g', 'u', 'i', 'd', 'o']


Hi Matt,

Cool!  Can you make the character rotation wrap around?  At the moment,
the letter 'u' is getting rotated to '\x82', but it should really wrap
around to 'h'.


You may find the remainder operator '%' useful to do the wrap-around:
###
>>> for i in range(10):
...     print i % 3
...
0
1
2
0
1
2
0
1
2
0
###


Good luck!




More information about the Tutor mailing list