Compare commits
2 Commits
aufgabe-oo
...
aufgabe-3-
| Author | SHA1 | Date | |
|---|---|---|---|
| 110f298bbf | |||
| 7624caaf6c |
17
README.md
17
README.md
@ -1,14 +1,3 @@
|
|||||||
# Assignment 2 - Caesar Cipher
|
The `Animal` base class sets up the `name` attribute.
|
||||||
|
- The `Cat` class inherits from `Animal` so it automatically gets the name property.
|
||||||
This is my code for the OOP homework. I chose the Caesar Cipher Helper.
|
- I added the `speak` method to the `Cat` class to make it return a str.
|
||||||
|
|
||||||
- **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,25 +1,8 @@
|
|||||||
class CaesarCipher:
|
class Animal:
|
||||||
|
def __init__(self, name:str):
|
||||||
|
self.name=name
|
||||||
|
|
||||||
def __init__(self, shift: int):
|
class Cat(Animal):
|
||||||
self.shift = shift
|
|
||||||
self.alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
||||||
|
|
||||||
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)
|
|
||||||
Loading…
x
Reference in New Issue
Block a user