Skip to content

Commit a23b0f5

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 47adce5 commit a23b0f5

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

ciphers/columnar_transposition_cipher.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import math
22

3+
34
def encrypt_columnar_cipher(message: str, key: str) -> str:
45
"""
56
Encrypts a message using the Columnar Transposition Cipher.
6-
7+
78
:param message: Text to encrypt.
89
:param key: String key used to define column order.
910
:return: Encrypted message.
@@ -12,23 +13,23 @@ def encrypt_columnar_cipher(message: str, key: str) -> str:
1213
message = message.replace(" ", "")
1314
num_cols = len(key)
1415
num_rows = math.ceil(len(message) / num_cols)
15-
16+
1617
# Fill the grid with characters
17-
grid = [''] * num_cols
18+
grid = [""] * num_cols
1819
for i, char in enumerate(message):
1920
grid[i % num_cols] += char
2021

2122
# Sort columns based on the key order
2223
sorted_key_indices = sorted(range(len(key)), key=lambda k: key[k])
23-
ciphertext = ''.join([grid[i] for i in sorted_key_indices])
24+
ciphertext = "".join([grid[i] for i in sorted_key_indices])
2425

2526
return ciphertext
2627

2728

2829
def decrypt_columnar_cipher(ciphertext: str, key: str) -> str:
2930
"""
3031
Decrypts a message encrypted with the Columnar Transposition Cipher.
31-
32+
3233
:param ciphertext: Encrypted text.
3334
:param key: String key used to define column order.
3435
:return: Decrypted message.
@@ -41,17 +42,17 @@ def decrypt_columnar_cipher(ciphertext: str, key: str) -> str:
4142
sorted_key_indices = sorted(range(len(key)), key=lambda k: key[k])
4243
col_lengths = [num_rows] * num_cols
4344
for i in range(num_shaded_boxes):
44-
col_lengths[sorted_key_indices[-(i+1)]] -= 1
45+
col_lengths[sorted_key_indices[-(i + 1)]] -= 1
4546

4647
# Distribute ciphertext into columns based on the sorted key
4748
grid = []
4849
start = 0
4950
for col_length in col_lengths:
50-
grid.append(ciphertext[start:start + col_length])
51+
grid.append(ciphertext[start : start + col_length])
5152
start += col_length
5253

5354
# Rebuild plaintext row by row
54-
plaintext = ''
55+
plaintext = ""
5556
for i in range(num_rows):
5657
for j in range(num_cols):
5758
if i < len(grid[sorted_key_indices[j]]):

0 commit comments

Comments
 (0)