rot13 in a more Pythonic style?

Andy Dingley dingbat at codesmiths.com
Fri Feb 16 13:31:58 EST 2007


On 14 Feb, 20:06, "Beej" <b... at beej.us> wrote:

> http://linuxgazette.net/109/pramode.html

Thanks, that's a _really_ interesting link   (Oh, I need to learn
Scheme!)

My code now looks like this, which I'm starting to feel much happier
about in a functional sense.


c_rot = lambda c, chars : (chr((ord(c) - ord(chars[0]) +
(len(chars) // 2)) % len(chars) + ord(chars[0])))

c_rot13 = lambda c : (((c, \
    c_rot(c, string.ascii_uppercase)) [c in string.ascii_uppercase]),
\
    c_rot(c, string.ascii_lowercase)) [c in string.ascii_lowercase]

rot13 = lambda s : string.join([ c_rot13(c) for c in s ],'')



I also considered this, but don't trust it as it relies on the two
sets being mutually disjoint between their inputs and outputs.

qc_rot = lambda c, chars : (c, c_rot(c, chars)) [c in chars]
c_rot13 = lambda c : (qc_rot( qc_rot(c, string.ascii_lowercase),
string.ascii_uppercase))





More information about the Python-list mailing list