|
1 | 1 | __author__ = 'ipetrash' |
2 | 2 |
|
3 | 3 |
|
4 | | -"""Скрипт реализует простейшую реализацию алгоритма Код Цезаря.""" |
| 4 | +"""Реализация алгоритма Код Цезаря.""" |
5 | 5 |
|
6 | 6 |
|
7 | | -def caesar_code(text, shift): |
8 | | - """Функция принимает текстовую строку и возвращает, новую строку |
9 | | - символы которой сдвинуты по алфавиту.""" |
| 7 | +from string import ascii_lowercase, ascii_uppercase |
| 8 | +ru_lowercase = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя' |
| 9 | +ru_uppercase = ru_lowercase.upper() |
| 10 | + |
| 11 | +alphabet_list = [ |
| 12 | + ascii_lowercase, |
| 13 | + ascii_uppercase, |
| 14 | + ru_lowercase, |
| 15 | + ru_uppercase, |
| 16 | +] |
10 | 17 |
|
11 | | - alp = [chr(c) for c in range(ord('a'), ord('z') + 1)] |
12 | | - new_t = '' |
13 | | - for c in text.lower(): |
14 | | - if c in alp: |
15 | | - i = (alp.index(c) + shift) % len(alp) |
16 | | - new_t += alp[i] |
17 | | - else: |
18 | | - new_t += c |
19 | | - return new_t |
20 | 18 |
|
| 19 | +def get_alp_by_char(char: str) -> list: |
| 20 | + for alphabet in alphabet_list: |
| 21 | + if char in alphabet: |
| 22 | + return alphabet |
21 | 23 |
|
22 | | -from string import ascii_uppercase |
| 24 | + return None |
23 | 25 |
|
24 | 26 |
|
25 | | -def shift(text, num): |
| 27 | +def caesar_code(text, shift): |
| 28 | + """Функция принимает текстовую строку и возвращает, новую строку |
| 29 | + символы которой сдвинуты по алфавиту.""" |
| 30 | + |
26 | 31 | shift_text = '' |
27 | 32 |
|
28 | 33 | for c in text: |
29 | | - if c not in ascii_uppercase: |
| 34 | + alphabet = get_alp_by_char(c) |
| 35 | + if alphabet is None: |
30 | 36 | shift_text += c |
31 | 37 | continue |
32 | 38 |
|
33 | | - i = (ascii_uppercase.index(c) + num) % len(ascii_uppercase) |
34 | | - shift_text += ascii_uppercase[i] |
| 39 | + i = (alphabet.index(c) + shift) % len(alphabet) |
| 40 | + shift_text += alphabet[i] |
35 | 41 |
|
36 | 42 | return shift_text |
37 | 43 |
|
38 | 44 |
|
39 | 45 | if __name__ == '__main__': |
40 | | - print(caesar_code("Hello World!", shift=2)) |
41 | | - print(caesar_code("Hello World!", shift=-4)) |
42 | | - print(caesar_code("Hello World!", shift=26)) |
43 | | - print(caesar_code("Hello World!", shift=50)) |
44 | | - print(caesar_code("Hello World!", shift=78)) |
| 46 | + text = "Hello World!" |
| 47 | + print(caesar_code(text, shift=0)) |
| 48 | + print(caesar_code(text, shift=2)) |
| 49 | + print(caesar_code(text, shift=-4)) |
| 50 | + print(caesar_code(text, shift=26)) |
| 51 | + print(caesar_code(text, shift=50)) |
| 52 | + print(caesar_code(text, shift=78)) |
| 53 | + |
| 54 | + assert caesar_code(text, shift=0) == text |
| 55 | + assert caesar_code(text, shift=2) == 'Jgnnq Yqtnf!' |
| 56 | + assert caesar_code(text, shift=-4) == 'Dahhk Sknhz!' |
| 57 | + assert caesar_code(text, shift=26) == 'Hello World!' |
| 58 | + assert caesar_code(text, shift=50) == 'Fcjjm Umpjb!' |
| 59 | + assert caesar_code(text, shift=78) == 'Hello World!' |
45 | 60 |
|
46 | 61 | print() |
47 | 62 |
|
48 | | - TEXT = 'VWDQ LV QRW ZKDW KH VHHPV' |
| 63 | + text = "Привет мир!" |
| 64 | + print(caesar_code(text, shift=0)) |
| 65 | + print(caesar_code(text, shift=2)) |
| 66 | + print(caesar_code(text, shift=-4)) |
| 67 | + print(caesar_code(text, shift=33)) |
| 68 | + print(caesar_code(text, shift=50)) |
| 69 | + print(caesar_code(text, shift=99)) |
| 70 | + |
| 71 | + assert caesar_code(text, shift=0) == text |
| 72 | + assert caesar_code(text, shift=2) == 'Сткджф окт!' |
| 73 | + assert caesar_code(text, shift=-4) == 'Лмеюбо ием!' |
| 74 | + assert caesar_code(text, shift=33) == 'Привет мир!' |
| 75 | + assert caesar_code(text, shift=50) == 'Абщтхг эщб!' |
| 76 | + assert caesar_code(text, shift=99) == 'Привет мир!' |
| 77 | + |
| 78 | + print() |
| 79 | + text = "Hello мир!" |
| 80 | + print(caesar_code(text, shift=0)) |
| 81 | + print(caesar_code(text, shift=2)) |
| 82 | + print(caesar_code(text, shift=-4)) |
| 83 | + |
| 84 | + assert caesar_code(text, shift=0) == text |
| 85 | + assert caesar_code(text, shift=2) == 'Jgnnq окт!' |
| 86 | + assert caesar_code(text, shift=-4) == 'Dahhk ием!' |
49 | 87 |
|
| 88 | + # Hint: see shift=23 |
| 89 | + print() |
| 90 | + print() |
| 91 | + TEXT = 'VWDQ LV QRW ZKDW KH VHHPV' |
50 | 92 | for i in range(len(ascii_uppercase)): |
51 | | - print(i, shift(TEXT, i)) |
| 93 | + print(i, caesar_code(TEXT, i)) |
0 commit comments