Skip to content

Commit 9c9da8e

Browse files
yanvoipre-commit-ci[bot]rohan472000cclausstianyizheng02
authored
Improve readability of ciphers/mixed_keyword_cypher.py (TheAlgorithms#8626)
* refactored the code * the code will now pass the test * looked more into it and fixed the logic * made the code easier to read, added comments and fixed the logic * got rid of redundant code + plaintext can contain chars that are not in the alphabet * fixed the reduntant conversion of ascii_uppercase to a list * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * keyword and plaintext won't have default values * ran the ruff command * Update linear_discriminant_analysis.py and rsa_cipher.py (TheAlgorithms#8680) * Update rsa_cipher.py by replacing %s with {} * Update rsa_cipher.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update linear_discriminant_analysis.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update linear_discriminant_analysis.py * Update linear_discriminant_analysis.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update linear_discriminant_analysis.py * Update linear_discriminant_analysis.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update linear_discriminant_analysis.py * Update machine_learning/linear_discriminant_analysis.py Co-authored-by: Christian Clauss <[email protected]> * Update linear_discriminant_analysis.py * updated --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]> * fixed some difficulties * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * added comments, made printing mapping optional, added 1 test * shortened the line that was too long * Update ciphers/mixed_keyword_cypher.py Co-authored-by: Tianyi Zheng <[email protected]> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Rohan Anand <[email protected]> Co-authored-by: Christian Clauss <[email protected]> Co-authored-by: Tianyi Zheng <[email protected]>
1 parent 7775de0 commit 9c9da8e

File tree

1 file changed

+53
-47
lines changed

1 file changed

+53
-47
lines changed

ciphers/mixed_keyword_cypher.py

+53-47
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
def mixed_keyword(key: str = "college", pt: str = "UNIVERSITY") -> str:
2-
"""
1+
from string import ascii_uppercase
2+
33

4-
For key:hello
4+
def mixed_keyword(
5+
keyword: str, plaintext: str, verbose: bool = False, alphabet: str = ascii_uppercase
6+
) -> str:
7+
"""
8+
For keyword: hello
59
610
H E L O
711
A B C D
@@ -12,58 +16,60 @@ def mixed_keyword(key: str = "college", pt: str = "UNIVERSITY") -> str:
1216
Y Z
1317
and map vertically
1418
15-
>>> mixed_keyword("college", "UNIVERSITY") # doctest: +NORMALIZE_WHITESPACE
19+
>>> mixed_keyword("college", "UNIVERSITY", True) # doctest: +NORMALIZE_WHITESPACE
1620
{'A': 'C', 'B': 'A', 'C': 'I', 'D': 'P', 'E': 'U', 'F': 'Z', 'G': 'O', 'H': 'B',
1721
'I': 'J', 'J': 'Q', 'K': 'V', 'L': 'L', 'M': 'D', 'N': 'K', 'O': 'R', 'P': 'W',
1822
'Q': 'E', 'R': 'F', 'S': 'M', 'T': 'S', 'U': 'X', 'V': 'G', 'W': 'H', 'X': 'N',
1923
'Y': 'T', 'Z': 'Y'}
2024
'XKJGUFMJST'
25+
26+
>>> mixed_keyword("college", "UNIVERSITY", False) # doctest: +NORMALIZE_WHITESPACE
27+
'XKJGUFMJST'
2128
"""
22-
key = key.upper()
23-
pt = pt.upper()
24-
temp = []
25-
for i in key:
26-
if i not in temp:
27-
temp.append(i)
28-
len_temp = len(temp)
29-
# print(temp)
30-
alpha = []
31-
modalpha = []
32-
for j in range(65, 91):
33-
t = chr(j)
34-
alpha.append(t)
35-
if t not in temp:
36-
temp.append(t)
37-
# print(temp)
38-
r = int(26 / 4)
39-
# print(r)
40-
k = 0
41-
for _ in range(r):
42-
s = []
43-
for _ in range(len_temp):
44-
s.append(temp[k])
45-
if k >= 25:
46-
break
47-
k += 1
48-
modalpha.append(s)
49-
# print(modalpha)
50-
d = {}
51-
j = 0
52-
k = 0
53-
for j in range(len_temp):
54-
for m in modalpha:
55-
if not len(m) - 1 >= j:
56-
break
57-
d[alpha[k]] = m[j]
58-
if not k < 25:
29+
keyword = keyword.upper()
30+
plaintext = plaintext.upper()
31+
alphabet_set = set(alphabet)
32+
33+
# create a list of unique characters in the keyword - their order matters
34+
# it determines how we will map plaintext characters to the ciphertext
35+
unique_chars = []
36+
for char in keyword:
37+
if char in alphabet_set and char not in unique_chars:
38+
unique_chars.append(char)
39+
# the number of those unique characters will determine the number of rows
40+
num_unique_chars_in_keyword = len(unique_chars)
41+
42+
# create a shifted version of the alphabet
43+
shifted_alphabet = unique_chars + [
44+
char for char in alphabet if char not in unique_chars
45+
]
46+
47+
# create a modified alphabet by splitting the shifted alphabet into rows
48+
modified_alphabet = [
49+
shifted_alphabet[k : k + num_unique_chars_in_keyword]
50+
for k in range(0, 26, num_unique_chars_in_keyword)
51+
]
52+
53+
# map the alphabet characters to the modified alphabet characters
54+
# going 'vertically' through the modified alphabet - consider columns first
55+
mapping = {}
56+
letter_index = 0
57+
for column in range(num_unique_chars_in_keyword):
58+
for row in modified_alphabet:
59+
# if current row (the last one) is too short, break out of loop
60+
if len(row) <= column:
5961
break
60-
k += 1
61-
print(d)
62-
cypher = ""
63-
for i in pt:
64-
cypher += d[i]
65-
return cypher
62+
63+
# map current letter to letter in modified alphabet
64+
mapping[alphabet[letter_index]] = row[column]
65+
letter_index += 1
66+
67+
if verbose:
68+
print(mapping)
69+
# create the encrypted text by mapping the plaintext to the modified alphabet
70+
return "".join(mapping[char] if char in mapping else char for char in plaintext)
6671

6772

6873
if __name__ == "__main__":
74+
# example use
6975
print(mixed_keyword("college", "UNIVERSITY"))

0 commit comments

Comments
 (0)