easy +10 pts

ROT13 Transform

Implement the classic Caesar cipher variant that shifts letters by 13 positions.

Write a function `rot13(s: str) -> str` that takes a string `s` and returns the ROT13 transformation. ROT13 replaces each English letter with the letter 13 positions after it in the alphabet, wrapping around. Only the 26 lowercase and 26 uppercase letters are transformed; all other characters (digits, spaces, punctuation) are left unchanged. The case of each letter must be preserved. For example, 'a' becomes 'n', 'N' becomes 'A', and 'hello' becomes 'uryyb'. Your function must be implemented from scratch; you may not use the built-in `codecs` module's ROT13 encoding or any external library.

Constraints

- 0 <= len(s) <= 10^5 - Characters are printable ASCII, including letters, digits, spaces, and common punctuation. - Expected time complexity: O(n) where n is the length of s. - Expected space complexity: O(n) for the output string.

Example

>>> rot13('Hello, World!')
'Uryyb, Jbeyq!'
>>> rot13('abcXYZ123')
'nopKLM123'
>>> rot13('')
''
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use `ord()` and `chr()` to shift character codes.
Handle lowercase and uppercase letters separately; wrapping occurs when the shifted code goes beyond 'z' or 'Z'.
Non-letter characters can be passed through unchanged.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.