diff --git a/src/codewars/kata_ceasar_cipher_helper.py b/src/codewars/kata_ceasar_cipher_helper.py index e69de29..f9d6643 100644 --- a/src/codewars/kata_ceasar_cipher_helper.py +++ b/src/codewars/kata_ceasar_cipher_helper.py @@ -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] + ) diff --git a/tests/codewars/test_ceasar_cipher_helper.py b/tests/codewars/test_ceasar_cipher_helper.py index e69de29..70efd3b 100644 --- a/tests/codewars/test_ceasar_cipher_helper.py +++ b/tests/codewars/test_ceasar_cipher_helper.py @@ -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" diff --git a/tests/codewars/test_vigenere_cipher_helper.py b/tests/codewars/test_vigenere_cipher_helper.py index 56c965f..a851339 100644 --- a/tests/codewars/test_vigenere_cipher_helper.py +++ b/tests/codewars/test_vigenere_cipher_helper.py @@ -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():