Skip to content

Commit a282984

Browse files
authored
from string import ascii_uppercase
1 parent 5a94998 commit a282984

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

ciphers/gronsfeld_cipher.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
https://www.dcode.fr/gronsfeld-cipher
88
"""
9+
from string import ascii_uppercase
910

1011

1112
def gronsfeld(text: str, key: str) -> str:
@@ -17,17 +18,16 @@ def gronsfeld(text: str, key: str) -> str:
1718
>>> gronsfeld('', '123')
1819
''
1920
"""
20-
alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
21-
keys = [int(i) for i in key]
21+
ascii_len = len(ascii_uppercase)
22+
key_len = len(key)
2223
encrypted_text = ""
24+
keys = [int(char) for char in key]
2325
upper_case_text = text.upper()
2426

2527
for i, char in enumerate(upper_case_text):
2628
if char in alphabets:
27-
new_position = (alphabets.index(char) + keys[i % len(keys)]) % len(
28-
alphabets
29-
)
30-
shifted_letter = alphabets[new_position]
29+
new_position = (ascii_uppercase.index(char) + keys[i % key_len]) % ascii_len
30+
shifted_letter = ascii_uppercase[new_position]
3131
encrypted_text += shifted_letter
3232
else:
3333
encrypted_text += char

0 commit comments

Comments
 (0)