readme
This commit is contained in:
parent
4fe95ea1a0
commit
f55a891b22
14
README.md
14
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.
|
||||
@ -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."
|
||||
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)
|
||||
Loading…
x
Reference in New Issue
Block a user