LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ' def letter_to_num(c: str) -> int: """Return the number that corresponds to the given letter. Preconditions: - len(c) == 1 and c in LETTERS """ return str.index(LETTERS, c) def num_to_letter(n: int) -> str: """Return the letter that corresponds to the given number. Precondtions: - 0 <= n < len(LETTERS) """ return LETTERS[n] def encrypt_caesar(k: int, plaintext: str) -> str: """Return the encrypted message using the Caesar cipher with key k. Preconditions: - all({x in LETTERS for x in plaintext}) - 1 <= k <= 26 """ ciphertext = '' for letter in plaintext: ciphertext = ciphertext + num_to_letter((letter_to_num(letter) + k) % len(LETTERS)) return ciphertext def decrypt_caesar(k: int, ciphertext: str) -> str: """Return the decrypted message using the Caesar cipher with key k. Preconditions: - all({x in LETTERS for x in ciphertext}) - 1 <= k <= 26 """ plaintext = '' for letter in ciphertext: plaintext = plaintext + num_to_letter((letter_to_num(letter) - k) % len(LETTERS)) return plaintext