Advertisement
Guest User

caesar.py

a guest
Jan 17th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. def caesar_cipher(_str, shift):
  2. alphabet = ["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"]
  3. numbers = [str(x) for x in range(10)] # creates an array of numbers 0-9
  4. _str = list(_str.lower()) # makes input string a list
  5.  
  6. for i in range(len(_str)):
  7. if _str[i] in alphabet:
  8. helper(alphabet, i, shift, _str)
  9. elif _str[i] in numbers:
  10. helper(numbers, i, shift, _str)
  11.  
  12. return ''.join(_str)
  13.  
  14. def helper(arr, i, shift, _arr):
  15. index = arr.index(_arr[i])
  16. while index+shift > len(arr)-1:
  17. shift -= len(arr)
  18. while index+shift < 0:
  19. shift += len(arr)
  20. _arr[i] = arr[index+shift]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement