easy +10 pts

Caesar Cipher Decrypt

Decrypt a message shifted by a given Caesar cipher key.

Implement a function `caesar_decrypt(ciphertext: str, key: int) -> str` that takes a string `ciphertext` and an integer `key`. The ciphertext contains only lowercase English letters and spaces. Decrypt it by shifting each lowercase letter backward by `key` positions in the alphabet (i.e., a→z for key=1, b→a for key=1, etc.). Spaces remain unchanged. The key is always between 0 and 25 inclusive. If the key is 0, return the original string. For example, with key=1, 'bcd' -> 'abc', 'z' -> 'y'. The function should return the decrypted string.

Constraints

Input string length: 0 ≤ len(ciphertext) ≤ 1000. Key: 0 ≤ key ≤ 25. The string consists of lowercase 'a'-'z' and spaces only. Time complexity O(n), where n is the length of the string.

Example

>>> caesar_decrypt('ibm', 1)
'hal'
>>> caesar_decrypt('khoor', 3)
'hello'
>>> caesar_decrypt('abc', 0)
'abc'
>>> caesar_decrypt('z', 1)
'y'
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use the ASCII code of each character: ord('a') is 97.
For each character, subtract key and use modulo 26 to wrap around.
Use chr() to convert the resulting code back to a character.
Remember to leave spaces as-is.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.