Compare commits
3 Commits
ee9265d00c
...
252ff429d2
| Author | SHA1 | Date | |
|---|---|---|---|
| 252ff429d2 | |||
| 696c37396b | |||
| 76e5f47d64 |
@ -1,9 +1,40 @@
|
|||||||
class VigenereCipher(object):
|
class VigenereCipher(object):
|
||||||
def __init__(self, key, alphabet):
|
def __init__(self, key, alphabet):
|
||||||
pass
|
self.key = key
|
||||||
|
self.alphabet = alphabet
|
||||||
|
|
||||||
def encode(self, text):
|
def encode(self, text):
|
||||||
pass
|
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):
|
def decode(self, text):
|
||||||
pass
|
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)
|
||||||
|
|||||||
@ -1,5 +1,16 @@
|
|||||||
"""from src.codewars.kata_cipher_helper import *
|
from src.codewars.kata_cipher_helper import VigenereCipher
|
||||||
|
|
||||||
|
|
||||||
def test_cipher_helper():
|
def test_cipher_helper():
|
||||||
pass"""
|
abc = "abcdefghijklmnopqrstuvwxyz"
|
||||||
|
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