Compare commits

...

3 Commits

4 changed files with 2 additions and 41 deletions

View File

@ -20,4 +20,5 @@ Repository for CDS-2020 Programming and Promt Engineering II
|Find the force of gravity between two objects|kata_force_of_gravity.py|test_force_of_gravity.py|[5b609ebc8f47bd595e000627](https://www.codewars.com/kata/5b609ebc8f47bd595e000627/)|
|The Lamp: Revisited|kata_the_lamp.py|test_the_lamp.py|[570e6e32de4dc8a8340016dd](https://www.codewars.com/kata/570e6e32de4dc8a8340016dd)|
|OOP: Object Oriented Piracy|kata_object_oriented_piracy.py|test_object_oriented_piracy.py|[54fe05c4762e2e3047000add](https://www.codewars.com/kata/54fe05c4762e2e3047000add)|
|Vigenère Cipher Helper|kata_cipher_helper.py|test_cipher_helper.py|[52d1bd3694d26f8d6e0000d3](https://www.codewars.com/kata/52d1bd3694d26f8d6e0000d3)|
|Vigenère Cipher Helper|kata_vigenere_cipher_helper.py|test_vigenere_cipher_helper.py|[52d1bd3694d26f8d6e0000d3](https://www.codewars.com/kata/52d1bd3694d26f8d6e0000d3)|
|Caesar Cipher Helper|kata_ceasar_cipher_helper.py|test_ceasar_cipher_helper.py|[526d42b6526963598d0004db](https://www.codewars.com/kata/526d42b6526963598d0004db)|

View File

@ -1,40 +0,0 @@
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)