From f55a891b22dc161444c41e6dbe9a899b6f8922fe Mon Sep 17 00:00:00 2001 From: hizlanarif Date: Sat, 6 Jun 2026 14:15:05 +0200 Subject: [PATCH] readme --- README.md | 14 ++++++++++++++ src/oop_solution.py | 29 +++++++++++++++++++++++------ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e69de29..df20fc2 100644 --- a/README.md +++ b/README.md @@ -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. \ No newline at end of file diff --git a/src/oop_solution.py b/src/oop_solution.py index 60b35ed..42ea9a5 100644 --- a/src/oop_solution.py +++ b/src/oop_solution.py @@ -1,8 +1,25 @@ -class Animal: - def __init__(self, name): - self.name = name +class CaesarCipher: + def __init__(self, shift: int): + self.shift = shift + self.alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" -class Cat(Animal): - def speak(self) -> str: - return f"{self.name} meows." \ No newline at end of file + def encode(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) + + 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) \ No newline at end of file