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
36 changes: 11 additions & 25 deletions rotational-cipher/rotational_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,18 @@ def rotate(text: str, key: int) -> str:
"""
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
if not char.isalpha():
new_str.append(char)
else:
new_str.append(replace_char(char, key))
new_str.append(char)
# Convert to a string
return "".join(new_str)


def replace_char(char: str, key: int) -> str:
"""
Shifts the character by the specified key positions within its alphabet
(uppercase or lowercase). Uses modular arithmetic to wrap around the
alphabet.

:param char: The alphabetic character to rotate
:param key: The number of positions to shift the character
:return: The rotated character in the same case as the input
"""
if char in LETTERS_UPPER:
new_index: int = LETTERS_UPPER.index(char) + key
if new_index < 26:
return LETTERS_UPPER[new_index]
return LETTERS_UPPER[new_index - 26]

new_index = LETTERS_LOWER.index(char) + key
if new_index < 26:
return LETTERS_LOWER[new_index]
return LETTERS_LOWER[new_index - 26]