Compare commits
No commits in common. "252ff429d2a7d582310b317afd37eafa5fb26acb" and "ee9265d00c1ebe7fd37ddf0f0c8ab91c925f8ab2" have entirely different histories.
252ff429d2
...
ee9265d00c
@ -1,40 +1,9 @@
|
|||||||
class VigenereCipher(object):
|
class VigenereCipher(object):
|
||||||
def __init__(self, key, alphabet):
|
def __init__(self, key, alphabet):
|
||||||
self.key = key
|
pass
|
||||||
self.alphabet = alphabet
|
|
||||||
|
|
||||||
def encode(self, text):
|
def encode(self, text):
|
||||||
encoded = []
|
pass
|
||||||
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):
|
def decode(self, text):
|
||||||
decoded = []
|
pass
|
||||||
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)
|
|
||||||
|
|||||||
@ -1,16 +1,5 @@
|
|||||||
from src.codewars.kata_cipher_helper import VigenereCipher
|
"""from src.codewars.kata_cipher_helper import *
|
||||||
|
|
||||||
|
|
||||||
def test_cipher_helper():
|
def test_cipher_helper():
|
||||||
abc = "abcdefghijklmnopqrstuvwxyz"
|
pass"""
|
||||||
key = "password"
|
|
||||||
c = VigenereCipher(key, abc)
|
|
||||||
|
|
||||||
assert c.encode("codewars") == "rovwsoiv"
|
|
||||||
assert c.decode("rovwsoiv") == "codewars"
|
|
||||||
|
|
||||||
assert c.encode("waffles") == "laxxhsj"
|
|
||||||
assert c.decode("laxxhsj") == "waffles"
|
|
||||||
|
|
||||||
assert c.encode("CODEWARS") == "CODEWARS"
|
|
||||||
assert c.decode("CODEWARS") == "CODEWARS"
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user