Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions solutions/python/rotational-cipher/2/rotational_cipher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
The rotational cipher, also sometimes called the Caesar cipher.

The Caesar cipher is a simple shift cipher that relies on transposing
all the letters in the alphabet using an integer key between 0 and 26.
Using a key of 0 or 26 will always yield the same output due to modular
arithmetic. The letter is shifted for as many values as the value of the key.
"""

import string

LETTERS_LOWER: str = string.ascii_lowercase
LETTERS_UPPER: str = string.ascii_uppercase


def rotate(text: str, key: int) -> str:
"""
Rotate each letter in the text by the specified key using Caesar cipher.

Non-alphabetic characters remain unchanged. The rotation wraps around
the alphabet (a->z, A->Z) using modular arithmetic.

:param text: The input string to be rotated
:param key: The number of positions to shift each letter (0-25)
:return: The rotated string with letters shifted by key positions

Examples:
>>> rotate("abc", 1)
"bcd"
>>> rotate("Hello, World!", 13)
"Uryyb, Jbeyq!"
"""
new_str: list[str] = []
for char in text:
# Lower case
if char in LETTERS_LOWER:
new_str.append(
LETTERS_LOWER[(LETTERS_LOWER.index(char) + key) % 26]
)
# Upper case
elif char in LETTERS_UPPER:
new_str.append(
LETTERS_UPPER[(LETTERS_UPPER.index(char) + key) % 26]
)
# Not a letter
else:
new_str.append(char)
# Convert to a string
return "".join(new_str)