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