feat: finished ceasar cipher kata and test. refractor: vigenere cipher testfile, updated import after renameing filename of kata src
This commit is contained in:
parent
d1f40c422a
commit
be6d0997dc
@ -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]
|
||||||
|
)
|
||||||
@ -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"
|
||||||
@ -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():
|
def test_cipher_helper():
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user