Раскритикуйте код по задаче
Суть задачи: шифратор-дешифратор по методу ришелье (буквы переставляются по заданному ключу, каждая цифра ключа является индексом буквы)
class Encode:
def __init__(self, text):
self.text = text
def splitToList(self):
import re
result = []
words = re.split(r'(?:[,|;|:|.|!|?|] | )', self.text)
for word in words:
sep = re.split(r'(?:;|,)', word)
if len(sep) > 1:
result.append(sep)
else:
result.append(list(word))
return result #some abcde --> [['s', 'o', 'm', 'e'], ['a', 'b', 'c', 'd', 'e']]
def randomKey(words):
import random
result = []
for word in words:
keyList = [num for num in range (0, len(word))] #[0, 1, 2, 3]
random.shuffle(keyList) #[2, 0, 1, 3]
result.append(list(keyList))
return result #[[2, 0, 1, 3], [0, 2, 3, 1, 4]]
def rishelye(words, keys, encode):
import copy
for i in range (len(words)):
word = words[i] #['s', 'o', 'm', 'e']
key = keys[i] #[2, 0, 1, 3]
encodeWord = copy.copy(word)
for j in range(len(word)): #'s'
index = int(key[j]) #int (2)
if encode == True:
encodeWord[j] = word[index] #some --> mome --> msme --> msoe
else:
encodeWord[index] = word[j]
yield encodeWord
def __repr__(self):
return self.text
if __name__ == '__main__':
text = Encode(str(input("Введите текст: "))) #обьекта класса
choose = str(input("[r to random key, d to decode]: "))
listText = Encode.splitToList(text)
if choose == 'r':
key = (Encode.randomKey(listText))
encode = True
else:
get_key = Encode(str(input("Введите ключ: ")))
key = Encode.splitToList(get_key)
encode = True
if choose == 'd':
encode = False
result = ''
for i in Encode.rishelye(listText, key, encode):
joinText = ''.join(i)
result += (joinText)
result += (' ')
resultKey = ''
for i in range (len(key)):
if len(key[i]) >= 11:
join = ','.join(map(str, key[i]))
else:
join = ''.join(map(str, key[i]))
resultKey += join
resultKey += (' ')
print ("\n\nИсходный текст: {}\nЗакодированный текст: {}\nКлюч: {}".format(text, result, resultKey))