I'm trying to write rot13, but to do it in a better and more Pythonic style than I'm currrently using. What would you reckon to the following pretty ugly thing? How would you improve it? In particular, I don't like the way a three-way selection is done by nesting two binary selections. Also I dislike stating the same algorithm twice, but can't see how to parameterise them neatly.
Yes, I know of .encode() and .translate(). No, I don't actually need rot13 itself, it's just a convenient substitute example for the real job-specific task. No, I don't have to do it with lambdas, but it would be nice if the final function was a lambda. #!/bin/python import string lc_rot13 = lambda c : (chr((ord(c) - ord('a') + 13) % 26 + ord('a'))) uc_rot13 = lambda c : (chr((ord(c) - ord('A') + 13) % 26 + ord('A'))) c_rot13 = lambda c : (((c, uc_rot13(c)) [c in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ']), lc_rot13(c) )[c in 'abcdefghijklmnopqrstuvwxyz'] rot13 = lambda s : string.join([ c_rot13(c) for c in s ],'') print rot13( 'Sybevk Tenohaqnr, Fcyhaqvt ihe guevtt' ) -- http://mail.python.org/mailman/listinfo/python-list