1

Question: How can you tell an extrovert from an introvert at NSA? Va gur ryringbef, gur rkgebireg ybbxf ng gur BGURE thl'f fubrf.

I found this joke on USENET, but the punchline is scrambled. Maybe you can decipher it? According to Wikipedia, ROT13 (http://en.wikipedia.org/wiki/ROT13) is frequently used to obfuscate jokes on USENET.

Hint: For this task you're only supposed to substitue characters. Not spaces, punctuation, numbers etc. Test examples:

My code:

def rot13(message):
    solved = ""
    for i in message:
        if i == "," or "-" or "/" or "." or "1" or "2" or "3" or "4" or "5" or "6" or "7" or "8" or "9":
            solved += i
        else:
            decipher = chr(ord(i)+13)
            solved += decipher
    return solved.upper()

problem I need solving Every time I try to change a letter past the 13th index it gives me a space instead of a letter. For example, when I put the letter "u" in the function it gives me a "_" but I want "h". How do I Cycle through the alphabet in Python instead of just ending on the 26th index

Addi
  • 49
  • 2
  • 5

2 Answers2

1

Try this:

import string

letters = string.ascii_letters
letters += letters  # now you can easily go for 'z' + 26, etc.

print ''.join( [letters[letters.find(k) + 13] if k in letters else k for k in msg] )

output:

in tHE ELEvAtors, tHE ExtrovErt LooKs At tHE OTheR Guy's sHoEs
lenik
  • 23,228
  • 4
  • 34
  • 43
0

You need to use the modulo operator: % in order to 'wrap-around' the alphabet.

(ord(i) + 13) % 26 = # value between 0 and 25, the substituted letter index

Modulo arithmetic is very important in crypto, and is used extensively.

mgrollins
  • 641
  • 3
  • 9