Add decrypt function to Caesar cipher
- Implement decrypt function that reverses encryption with negative shift - Export decrypt in package's public API - Update tests with basic roundtrip and edge case tests - Update documentation to reflect encryption/decryption support
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# 🔐 Caesar Cipher
|
||||
|
||||
A beginner-friendly Python program that encodes messages with the Caesar cipher.
|
||||
A beginner-friendly Python program that encodes and decodes messages with the Caesar cipher.
|
||||
|
||||
## 📚 Table of Contents
|
||||
|
||||
@@ -14,7 +14,7 @@ A beginner-friendly Python program that encodes messages with the Caesar cipher.
|
||||
|
||||
## 🧭 Overview
|
||||
|
||||
The Caesar cipher shifts every letter by a fixed number of places (with a shift of 3, `E` becomes `H`). This project, from Udemy's *100 Days of Code: The Complete Python Pro Bootcamp* (Day 8), finds letter positions with `list.index` and wraps around the alphabet with the modulo operator. Part 1 covers encryption; decryption and a combined program follow in later parts. Code lives in `src/`, tests in `tests/`, docs in `docs/`.
|
||||
The Caesar cipher shifts every letter by a fixed number of places (with a shift of 3, `E` becomes `H`). This project, from Udemy's *100 Days of Code: The Complete Python Pro Bootcamp* (Day 8), finds letter positions with `list.index` and wraps around the alphabet with the modulo operator. Parts 1 and 2 cover encryption and decryption; a combined program follows in Part 3. Code lives in `src/`, tests in `tests/`, docs in `docs/`.
|
||||
|
||||
## 📋 Requirements
|
||||
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@
|
||||
Each letter is moved `shift` places through the alphabet:
|
||||
`new_position = (ALPHABET.index(letter) + shift) % 26`.
|
||||
The modulo keeps positions in range for shifts past `z`.
|
||||
Non-letters are left untouched and letter case is preserved.
|
||||
Decryption is encryption with `-shift`. Non-letters are left untouched and letter case is preserved.
|
||||
|
||||
API reference: run `doxygen Doxyfile` from the repository root and open
|
||||
`docs/doxygen/html/index.html`. The step-by-step plan is in [plan.md](plan.md).
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
## @file __init__.py
|
||||
# @brief Public API of the caesar_cipher package.
|
||||
|
||||
from caesar_cipher.cipher import encrypt
|
||||
from caesar_cipher.cipher import decrypt, encrypt
|
||||
|
||||
__all__ = ["encrypt"]
|
||||
__all__ = ["encrypt", "decrypt"]
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
## @file cipher.py
|
||||
# @brief Caesar cipher encryption.
|
||||
# @brief Caesar cipher encryption and decryption.
|
||||
|
||||
from caesar_cipher.constants import ALPHABET
|
||||
|
||||
@@ -19,3 +19,11 @@ def encrypt(text: str, shift: int) -> str:
|
||||
else:
|
||||
result.append(char)
|
||||
return "".join(result)
|
||||
|
||||
|
||||
def decrypt(text: str, shift: int) -> str:
|
||||
## @brief Decrypt a Caesar-cipher text.
|
||||
# @param text Encrypted text.
|
||||
# @param shift Shift that was used to encrypt.
|
||||
# @return The original text.
|
||||
return encrypt(text, -shift)
|
||||
|
||||
+14
-1
@@ -1,4 +1,4 @@
|
||||
from caesar_cipher import encrypt
|
||||
from caesar_cipher import decrypt, encrypt
|
||||
from caesar_cipher.env import load_env
|
||||
|
||||
|
||||
@@ -24,3 +24,16 @@ def test_load_env(tmp_path):
|
||||
f.write_text("# c\nA=1\nB='two'\n", encoding="utf-8")
|
||||
assert load_env(f) == {"A": "1", "B": "two"}
|
||||
assert load_env(tmp_path / "missing") == {}
|
||||
|
||||
|
||||
def test_decrypt_basic():
|
||||
assert decrypt("mjqqt", 5) == "hello"
|
||||
|
||||
|
||||
def test_decrypt_roundtrip():
|
||||
assert decrypt(encrypt("Hello, World!", 7), 7) == "Hello, World!"
|
||||
|
||||
|
||||
def test_decrypt_wraps_around_alphabet():
|
||||
assert decrypt("abc", 3) == "xyz"
|
||||
assert decrypt("def", 29) == "abc"
|
||||
|
||||
Reference in New Issue
Block a user