-
-
Notifications
You must be signed in to change notification settings - Fork 47.9k
Improve readability of ciphers/mixed_keyword_cypher.py #8626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
518f4e5
16c9268
a6d64ff
642ccc3
3520c16
3a099f5
738f193
b284ab1
ad436ce
dbbd778
be6f043
33b34c4
66e28ee
35d3de2
0bf2ed2
05d0283
ad8c99f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
def mixed_keyword(key: str = "college", pt: str = "UNIVERSITY") -> str: | ||
""" | ||
from string import ascii_uppercase | ||
|
||
|
||
For key:hello | ||
def mixed_keyword(keyword: str = "college", plaintext: str = "UNIVERSITY") -> str: | ||
""" | ||
For keyword: hello | ||
|
||
H E L O | ||
A B C D | ||
|
@@ -19,50 +21,49 @@ def mixed_keyword(key: str = "college", pt: str = "UNIVERSITY") -> str: | |
'Y': 'T', 'Z': 'Y'} | ||
'XKJGUFMJST' | ||
""" | ||
key = key.upper() | ||
pt = pt.upper() | ||
temp = [] | ||
for i in key: | ||
if i not in temp: | ||
temp.append(i) | ||
len_temp = len(temp) | ||
# print(temp) | ||
alpha = [] | ||
modalpha = [] | ||
for j in range(65, 91): | ||
t = chr(j) | ||
alpha.append(t) | ||
if t not in temp: | ||
temp.append(t) | ||
# print(temp) | ||
r = int(26 / 4) | ||
# print(r) | ||
keyword = keyword.upper() | ||
plaintext = plaintext.upper() | ||
|
||
unique_chars = [] | ||
for char in keyword: | ||
if char not in unique_chars: | ||
unique_chars.append(char) | ||
|
||
num_unique_chars_in_keyword = len(unique_chars) | ||
|
||
alphabet = list(ascii_uppercase) | ||
# add the rest of the alphabet to the unique_chars list | ||
for char in alphabet: | ||
if char not in unique_chars: | ||
unique_chars.append(char) | ||
|
||
rows = int(26 / 4) | ||
# k is an index variable | ||
k = 0 | ||
for _ in range(r): | ||
s = [] | ||
for _ in range(len_temp): | ||
s.append(temp[k]) | ||
modified_alphabet = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do all this with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As this is a basic substitution cypher, it is bounded by the number of different letters in the alphabet we're using. Using sets in this case wouldn't change the performance almost at all. |
||
for _ in range(rows): | ||
row = [] | ||
for _ in range(num_unique_chars_in_keyword): | ||
row.append(unique_chars[k]) | ||
if k >= 25: | ||
break | ||
k += 1 | ||
modalpha.append(s) | ||
# print(modalpha) | ||
d = {} | ||
j = 0 | ||
modified_alphabet.append(row) | ||
|
||
mapping = {} | ||
k = 0 | ||
for j in range(len_temp): | ||
for m in modalpha: | ||
if not len(m) - 1 >= j: | ||
for j in range(num_unique_chars_in_keyword): | ||
for row in modified_alphabet: | ||
if not len(row) - 1 >= j: | ||
break | ||
d[alpha[k]] = m[j] | ||
mapping[alphabet[k]] = row[j] | ||
if not k < 25: | ||
break | ||
k += 1 | ||
print(d) | ||
cypher = "" | ||
for i in pt: | ||
cypher += d[i] | ||
return cypher | ||
|
||
# create the encrypted text by mapping the plaintext to the modified alphabet | ||
return "".join(mapping[char] for char in plaintext) | ||
|
||
|
||
print(mixed_keyword("college", "UNIVERSITY")) | ||
if __name__ == "__main__": | ||
print(mixed_keyword("college", "UNIVERSITY")) |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make this a
set
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it cannot be done with sets because the order of these characters in the keyword matters - it determines how we will map plaintext characters to the cyphertext.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then I think it would help if this was mentioned in the comments, so that future maintainers know that the char order matters