Из-за того что решение не продумал, а сразу начал фигачить код, то все очень сумбурно и непотяно почему.
Сейчас при запуске выкидывает исключение в строке
if first_symb[0] > second_symb[0]:
TypeError: 'NoneType' object is not subscriptable
Иногда код успешно выполняется - дает ожидаемый результат.
Сам код:
import string import random def group(iterable, count): """ Группировка элементов последовательности по count элементов """ return list(zip(*[iter(iterable)] * count)) def find_pos(symb, lst): if symb in lst: return lst.index[symb] def get_pos(symb, table): for i in range(len(table)): if symb in table[i]: return i, table[i].index(symb) def make_lst(table): for i in range(len(table)): table[i] = list(table[i]) return table def make_bigramms(phrs): bigramms = [] i = 0 while i < len(phrs): stop = i + 2 bigramms.append(phrase[i:stop]) i += 2 if len(bigramms[-1]) < 2: bigramms[-1].append('a') return bigramms def get_enc_symb(symb1, symb2, table1, table2): first_symb = get_pos(symb1, table1) second_symb = get_pos(symb2, table2) if first_symb[0] > second_symb[0]: first_enc_symb = table2[second_symb[0]-1][second_symb[1]] if first_symb[0] == len(table1): second_enc_symb = table1[first_symb[0]+1][first_symb[1]] elif first_symb[0] < second_symb[0]: first_enc_symb = table1[first_symb[0]-1][first_symb[1]] if first_symb[0] == len(table1): second_enc_symb = table2[second_symb[0]+1][second_symb[1]] elif first_symb[0] == second_symb[0]: first_enc_symb = table2[second_symb[0]][second_symb[1]] second_enc_symb = table1[first_symb[0]][first_symb[1]] return table1[first_symb[0]][first_symb[1]], table2[second_symb[0]][second_symb[1]] alphabet = string.ascii_lowercase chars_to_fill = list(alphabet) # список для заполнения таблиц phrase = 'People rarely succeed unless they have fun in what they are doing.'.lower() reserve = [' ', '-', ',', ':', ';', '.'] # список добавочных символов для заполнения таблиц for i in phrase.lower(): # формирование списка для заполнения if i not in alphabet: chars_to_fill.append(i) chars_to_fill = list(set(chars_to_fill)) while len(chars_to_fill) < 30: for i in reserve: if i not in chars_to_fill: chars_to_fill.append(i) table1 = make_lst(group(chars_to_fill, 5)) # генерация таблиц random.shuffle(chars_to_fill) table2 = make_lst(group(chars_to_fill, 5)) bigramms = make_bigramms(phrase) res = '' for i in bigramms: res += str(get_enc_symb(i[0], i[1], table1, table2)) print(res)