feat: finished ceasar cipher kata and test. refractor: vigenere cipher testfile, updated import after renameing filename of kata src

This commit is contained in:
Sandro Zimmermann 2026-03-04 23:55:54 +01:00
parent d1f40c422a
commit be6d0997dc
3 changed files with 37 additions and 1 deletions

View File

@ -0,0 +1,28 @@
class CaesarCipher(object):
def __init__(self, shift):
self.shift = shift
self.alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def encode(self, text):
idx_alph = [
self.alphabet.index(x) if x in self.alphabet else x for x in text.upper()
]
idx_shift = [
(x + self.shift) % len(self.alphabet) if isinstance(x, (int)) else x
for x in idx_alph
]
return "".join(
[self.alphabet[x] if isinstance(x, (int)) else x for x in idx_shift]
)
def decode(self, text):
idx_alph = [
self.alphabet.index(x) if x in self.alphabet else x for x in text.upper()
]
idx_shift = [
(x - self.shift) % len(self.alphabet) if isinstance(x, (int)) else x
for x in idx_alph
]
return "".join(
[self.alphabet[x] if isinstance(x, (int)) else x for x in idx_shift]
)

View File

@ -0,0 +1,8 @@
from src.codewars.kata_ceasar_cipher_helper import CaesarCipher
def test_cipher_helper():
c = CaesarCipher(5)
assert c.encode("Codewars") == "HTIJBFWX"
assert c.decode("HTIJBFWX") == "CODEWARS"

View File

@ -1,4 +1,4 @@
from src.codewars.kata_cipher_helper import VigenereCipher
from src.codewars.kata_vigenere_cipher_helper import VigenereCipher
def test_cipher_helper():