This commit is contained in:
Arif Hizlan 2026-06-06 14:15:05 +02:00
parent 4fe95ea1a0
commit f55a891b22
2 changed files with 37 additions and 6 deletions

View File

@ -0,0 +1,14 @@
# Assignment 2 - Caesar Cipher
This is my code for the OOP homework. I chose the Caesar Cipher Helper.
- **Link:** https://www.codewars.com/kata/526d42b6526963598d0004db
### How it works:
- The `CaesarCipher` class takes a `shift` number.
- `encode` changes text to uppercase and moves letters forward. It uses `% 26` so letters stay in the A-Z alphabet.
- `decode` moves letters backward to fix the text.
- Spaces and punctuation do not change.
### Tools:
I set up black and ruff with pre-commit hooks. They check and fix my code format before every commit.

View File

@ -1,8 +1,25 @@
class Animal: class CaesarCipher:
def __init__(self, name):
self.name = name
def __init__(self, shift: int):
self.shift = shift
self.alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
class Cat(Animal): def encode(self, st: str) -> str:
def speak(self) -> str: result = []
return f"{self.name} meows." for char in st.upper():
if char in self.alphabet:
new_index = (self.alphabet.index(char) + self.shift) % 26
result.append(self.alphabet[new_index])
else:
result.append(char)
return "".join(result)
def decode(self, st: str) -> str:
result = []
for char in st.upper():
if char in self.alphabet:
new_index = (self.alphabet.index(char) - self.shift) % 26
result.append(self.alphabet[new_index])
else:
result.append(char)
return "".join(result)