Rotonym, what the heck is a rotonym?
Tuesday, January 22nd, 2008Rotonyms are words that ROT-13 in to other words. I discovered the term from this site, which I found during my morning Reddit session. I think the author invented the term, because the only reference I found to ‘rotonym’ came from his site. Long story short, I knocked out a Python script to find all the rotonyms, given a list of words (just for the heck of it). I like problems like this. Its like the kind of work you had to do in high school and uni, before you enter the real word of programming and discover that most problems have blurry definitions and its hard to tell if your solution actualy works in all cases. Here’s the code, let me know if you have a better solution.
words = [line.rstrip() for line in open("WORD.LST")] words_set = set(words) letters = "abcdefghijklmnopqrstuvwxyz" repl = dict(zip(letters, letters[13:]+letters[:13])) def rot13(word): return "".join(repl.get(c, c) for c in word) found = [] for word in words: rotword = rot13(word) if rotword in words_set: found.append( (word, rotword) ) words_set.discard(word) found.sort( key=lambda words:(len(words[0]), words[0]) ) for word, rotword in found: print "%s\\t%s"%(word, rotword)
If you want to run it, you will need this word list. Its not the most interesting code you will see, but I was impressed at how much functionality Python squeezes in to each line, without making it too obfusticated. It would be interesting to see this solved in other languages for comparison.
Update: fs111 reminded me that there was a rot13 string encoding in Python. So the code becomes:
words = [line.rstrip() for line in open("WORD.LST")] words_set = set(words) found = [] for word in words: rotword = word.encode("rot13") if rotword in words_set: found.append( (word, rotword) ) words_set.discard(word) found.sort( key=lambda words:(len(words[0]), words[0]) ) for word, rotword in found: print "%s\\t%s"%(word, rotword)