Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import ceil
- def to_encrypt_6(s, key): #прямая изгородь
- answer = [''] * key
- dir = 1
- cur = 0
- for e in s:
- answer[cur] += e
- cur += dir
- if cur == 0 or cur == key - 1:
- dir *= -1
- return ''.join(answer)
- def to_encrypt_7(s, key): #обратная изгородь
- answer = [''] * key
- dir = -1
- cur = key - 1
- for e in s:
- answer[cur] += e
- cur += dir
- if cur == 0 or cur == key - 1:
- dir *= -1
- return ''.join(answer)
- def to_encrypt_8(s, key): #перестановочная прямая
- key = sorted([(key[i], i) for i in range(len(key))])
- answ = ''
- mx = []
- for i in range(0, len(s), len(key)):
- mx.append(s[i:i + len(key)])
- for e in mx:
- for ind in key:
- if ind[1] < len(e):
- answ += e[ind[1]]
- return answ
- def to_encrypt_9(s, key): #перестановочная обратная
- key = sorted([(key[i], i) for i in range(len(key))])
- mx = [''] * ceil(len(s) / len(key))
- cur = len(mx) - 1
- for i in range(0, len(s), len(key)):
- mx[cur] = s[i:i + len(key)][::-1]
- if len(mx[cur]) < len(key):
- mx[cur] = '0' * (len(key) - len(mx[cur])) + mx[cur]
- cur -= 1
- answ = ''
- for e in mx:
- for ind in key:
- if e[ind[1]] != '0':
- answ += e[ind[1]]
- return answ
- def to_encrypt_10(s, key): #redefence прямой
- answer = [''] * len(key)
- dir = 1
- cur = 0
- for e in s:
- answer[cur] += e
- cur += dir
- if cur == 0 or cur == len(key) - 1:
- dir *= -1
- key = sorted([(key[i], i) for i in range(len(key))])
- redefence = ''
- for e in key:
- redefence += answer[e[1]]
- return redefence
- def to_encrypt_11(s, key): #redefence обратный
- answer = [''] * len(key)
- dir = -1
- cur = len(key) - 1
- for e in s:
- answer[cur] += e
- cur += dir
- if cur == 0 or cur == len(key) - 1:
- dir *= -1
- key = sorted([(key[i], i) for i in range(len(key))])
- redefence = ''
- for e in key:
- redefence += answer[e[1]]
- return redefence
- print(to_encrypt_6('изгородь', 3))
- print(to_encrypt_7('изгородь', 3))
- print(to_encrypt_8('перестановка', 'слово'))
- print(to_encrypt_9('перестановка', 'слово'))
- print(to_encrypt_10('изгородь', 'сон'))
- print(to_encrypt_11('изгородь', 'сон'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement