Created
May 15, 2017 13:05
-
-
Save chrisbay/496880de24bba4f532ed03211eff7294 to your computer and use it in GitHub Desktop.
For Web Caesar assignment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def alphabet_position(character): | |
alphabet = 'abcdefghijklmnopqrstuvwxyz' | |
lower = character.lower() | |
return alphabet.index(lower) | |
def rotate_string_13(text): | |
rotated = '' | |
alphabet = 'abcdefghijklmnopqrstuvwxyz' | |
for char in text: | |
rotated_idx = (alphabet_position(char) + 13) % 26 | |
if char.isupper(): | |
rotated = rotated + alphabet[rotated_idx].upper() | |
else: | |
rotated = rotated + alphabet[rotated_idx] | |
return rotated | |
def rotate_character(char, rot): | |
alphabet = 'abcdefghijklmnopqrstuvwxyz' | |
rotated_idx = (alphabet_position(char) + rot) % 26 | |
if char.isupper(): | |
return alphabet[rotated_idx].upper() | |
else: | |
return alphabet[rotated_idx] | |
def rotate_string(text, rot): | |
rotated = '' | |
for char in text: | |
if (char.isalpha()): | |
rotated = rotated + rotate_character(char, rot) | |
else: | |
rotated = rotated + char | |
return rotated |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment