chore: renamed cipher_helper.py in vigenere_chpher_helper. Next kata will be ceasar_cipher_helper

This commit is contained in:
Sandro Zimmermann 2026-03-04 23:04:55 +01:00
parent 252ff429d2
commit d188900557
3 changed files with 40 additions and 0 deletions

View File

@ -0,0 +1,40 @@
class VigenereCipher(object):
def __init__(self, key, alphabet):
self.key = key
self.alphabet = alphabet
def encode(self, text):
encoded = []
idx_key = 0
for char in text:
if char in self.alphabet:
text_pos = self.alphabet.index(char)
key_char = self.key[idx_key % len(self.key)]
key_pos = self.alphabet.index(key_char)
pos = (text_pos + key_pos) % len(self.alphabet)
encoded.append(self.alphabet[pos])
else:
encoded.append(char)
idx_key += 1
return "".join(encoded)
def decode(self, text):
decoded = []
idx_key = 0
for char in text:
if char in self.alphabet:
text_pos = self.alphabet.index(char)
key_char = self.key[idx_key % len(self.key)]
key_pos = self.alphabet.index(key_char)
pos = (text_pos - key_pos) % len(self.alphabet)
decoded.append(self.alphabet[pos])
else:
decoded.append(char)
idx_key += 1
return "".join(decoded)