Advertisement
graemeblake

ROT13

Apr 30th, 2012
834
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.61 KB | None | 0 0
  1. def rot13(s):
  2.     """"
  3.    Takes a string and applies rot13. Returns rotated string.
  4.    Uses arrays of each letter + two dictionaries.
  5.    The arrays are used to check if the letter is upper or lower.
  6.    If the letter is upper or lowercase, the appropriate value is found in the dictionary,
  7.    and 12 is added.
  8.    If temp is over 26, 26 is subtracted.
  9.    """
  10.     lower = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
  11.     upper = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
  12.    
  13.     string = ''
  14.     ldict = {'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'g': 7, 'f': 6, 'i': 9, 'h': 8, 'k': 11, 'j': 10, 'm': 13, 'l': 12, 'o': 15, 'n': 14, 'q': 17, 'p': 16, 's': 19, 'r': 18, 'u': 21, 't': 20, 'w': 23, 'v': 22, 'y': 25, 'x': 24, 'z': 26}
  15.     updict = {'A': 1, 'C': 3, 'B': 2, 'E': 5, 'D': 4, 'G': 7, 'F': 6, 'I': 9, 'H': 8, 'K': 11, 'J': 10, 'M': 13, 'L': 12, 'O': 15, 'N': 14, 'Q': 17, 'P': 16, 'S': 19, 'R': 18, 'U': 21, 'T': 20, 'W': 23, 'V': 22, 'Y': 25, 'X': 24, 'Z': 26}
  16.     for letter in s:
  17.         if letter in lower:
  18.             temp = ldict[letter] + 12
  19.             if temp <= 25:
  20.                 string += lower[temp]
  21.             else:
  22.                 string += lower[temp - 26]
  23.         elif letter in upper:
  24.             temp = updict[letter] + 12
  25.             if temp <= 25:
  26.                 string += upper[temp]
  27.             else:
  28.                 string += upper[temp - 26]
  29.         else:
  30.             string += letter
  31.     return string
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement