From 88417a060abce1988f490e0004dc69bcafe2064a Mon Sep 17 00:00:00 2001 From: Samuel Weber Date: Tue, 3 Mar 2026 22:27:03 +0100 Subject: [PATCH] =?UTF-8?q?Aufgabe2,=20Codewars=20Uebung=20Vigen=C3=A8re?= =?UTF-8?q?=20Cipher=20Helper=20kyu4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Woche2/aufgabe2.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/Woche2/aufgabe2.py diff --git a/src/Woche2/aufgabe2.py b/src/Woche2/aufgabe2.py new file mode 100644 index 0000000..57d1ee3 --- /dev/null +++ b/src/Woche2/aufgabe2.py @@ -0,0 +1,41 @@ +def key_string(key, text): + keystring = key + while len(keystring) <= len(text): + keystring += key + return keystring[: len(text)] + + +class VigenereCipher(object): + def __init__(self, key, alphabet): + self.alphabet = alphabet + self.key = key + self.keymap = [] + for i in range(len(alphabet)): + self.keymap.append(alphabet) + alphabet = alphabet[1:] + alphabet[0] + + def encode(self, text): + keystring = key_string(self.key, text) + encode_string = "" + + for index, i in enumerate(text): + index_alpha = self.alphabet.find(i) + if index_alpha == -1: + encode_string += i + else: + index_key = self.alphabet.find(keystring[index]) + encode_string += self.keymap[index_key][index_alpha] + return encode_string + + def decode(self, text): + keystring = key_string(self.key, text) + decode_string = "" + + for index, i in enumerate(text): + if self.alphabet.find(i) == -1: + decode_string += i + else: + index_key = self.alphabet.find(keystring[index]) + index_alpha = self.keymap[index_key].find(i) + decode_string += self.alphabet[index_alpha] + return decode_string