easy +10 pts

ROT13 transform

Rotate every letter in a string by 13 positions in the alphabet.

Write a function `rot13(s)` that takes a string `s` and returns a new string where every alphabetic character is shifted by 13 positions in the alphabet (wrapping around), preserving the original case. Non-alphabetic characters (digits, spaces, punctuation, etc.) remain unchanged. The function must be case-sensitive: uppercase letters map to uppercase, lowercase to lowercase. For example, 'a' becomes 'n', 'A' becomes 'N', 'n' becomes 'a', and 'N' becomes 'A'. The input string may be empty and may contain any printable ASCII characters. The function must not use any external libraries or the built-in `str.translate` (if you know it), and must implement the logic manually.

Constraints

0 <= len(s) <= 10^5. Time complexity O(n), space complexity O(n) for the result.

Example

>>> rot13('Hello, World!')
'Uryyb, Jbeyq!'
>>> rot13('abcXYZ')
'nopKLM'
>>> rot13('123')
'123'
>>> rot13('')
''
10 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use Python's `ord()` and `chr()` to convert between characters and their ASCII codes.
Check if a character is in 'a'..'z' or 'A'..'Z' by comparing its ASCII value.
Shift: for a lowercase letter, new = (ord(c) - ord('a') + 13) % 26 + ord('a'); for uppercase use 'A'.
Build the result using a list of transformed characters and join at the end for efficiency.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.