Well, I came to a sudden realization shortly after my post on Saturday — because ROT13 is its own solver, I didn’t need any loops at all for encoding vs. decoding ROT13, just a single function 😳 . And even more horrifying was the next realization — I didn’t even need to have two separate ROT13 programs in the first place AT ALL! Wow. That makes me feel good about myself.
So… here is the “all I ever really needed to do” update,
#!/usr/bin/env python # rot13er.py v1.0.1 # ROT13er # # Changelog: # v1.0.1 (09/26/2010) + Cleaned up code # v1.0.0 (09/25/2010) + Initial release # * Combines ROT13 Encoder & ROT13 Decoder # # Copyright 2010 Benjamin Braithwaite # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # imports maketrans capability & generate ROT13 translation table from string import maketrans rot13table = maketrans("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",\ "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm") # user interface and encoding magics print "ROT13er v1.0.1" print "Copyright 2010 by Benjamin Braithwaite" print print "A simple ROT13 translator" print toberotted = "" while not toberotted.strip(): toberotted = raw_input("Enter your text (ROT13 or plaintext): ") print print toberotted.translate(rot13table) print # successful exit dialog raw_input ("Press <enter> to exit.")
It’s the simple things that get you.