+6
-5
python/oct2/level4/routeCipher2.py
+6
-5
python/oct2/level4/routeCipher2.py
···
1
1
from math import ceil
2
2
from string import ascii_lowercase
3
3
4
+
4
5
def getIndex(row: int, col: int, rows: int) -> int:
5
-
return col*rows + row
6
+
return col * rows + row
6
7
7
8
8
9
def getRowAndCol(x: int, rows: int) -> tuple[int, int]:
···
20
21
encoded_message = f"{rows}"
21
22
for i in range(0, rows):
22
23
if i % 2 == 0:
23
-
encoded_message += encoded_string[i*row_len:(i+1)*row_len]
24
+
encoded_message += encoded_string[i * row_len : (i + 1) * row_len]
24
25
else:
25
-
encoded_message += encoded_string[i*row_len:(i+1)*row_len][::-1]
26
+
encoded_message += encoded_string[i * row_len : (i + 1) * row_len][::-1]
26
27
return encoded_message
27
28
28
29
···
34
35
decoded_string = ""
35
36
for i in range(rows):
36
37
if i % 2 == 0:
37
-
decoded_string += encoded_string[i*row_len:(i+1)*row_len]
38
+
decoded_string += encoded_string[i * row_len : (i + 1) * row_len]
38
39
else:
39
-
decoded_string += encoded_string[i*row_len:(i+1)*row_len][::-1]
40
+
decoded_string += encoded_string[i * row_len : (i + 1) * row_len][::-1]
40
41
for i in range(len(decoded_string)):
41
42
row, col = getRowAndCol(i, rows)
42
43
decoded_message += decoded_string[getIndex(row, col, row_len)]