Created
August 28, 2018 21:44
-
-
Save samisnotinsane/e00d3f03ffd939c8f396a98248899f35 to your computer and use it in GitHub Desktop.
Reads contents from a text file and performs a rot13 encode on letters from the English alphabet.
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 join_letters_to_string(chars): | |
s = "" | |
for c in chars: | |
s += c | |
return s | |
def rotate(letter, position): | |
# Assumptions: | |
# -input is a letter (i.e. length = 1) | |
# -input is an alphabetical character (i.e. no punctuations) | |
letter_unicode = ord(letter) | |
letter_rotated_unicode = letter_unicode + position | |
# Loopback after Z/z. | |
if letter_rotated_unicode > ord("Z") and letter.isupper(): | |
letter_rotated_unicode = ( ((letter_unicode + position) - ord("Z")) + (ord("A") - 1) ) | |
elif letter_rotated_unicode > ord("z") and letter.islower(): | |
letter_rotated_unicode = ( ((letter_unicode + position) - ord("z")) + (ord("a") - 1) ) | |
letter_rotated = chr(letter_rotated_unicode) | |
return letter_rotated | |
def rot_13(word): | |
rotated_letters = [] | |
for letter in word: | |
rotated_letter = rotate(letter, 13) | |
rotated_letters.append(rotated_letter) | |
return join_letters_to_string(rotated_letters) | |
def main(): | |
f = open("input.txt", "r") | |
words = f.readline().strip().split() | |
sentence = "" | |
for i in range(len(words)): | |
r_word = rot_13(words[i]) | |
if i != (len(words)-1): | |
sentence += r_word + " " | |
else: | |
sentence += r_word | |
print(sentence) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment