-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
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
Conversation
ciphers/mixed_keyword_cypher.py
Outdated
unique_chars = [] | ||
for char in keyword: | ||
if char not in unique_chars: | ||
unique_chars.append(char) |
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.
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.
Then I think it would help if this was mentioned in the comments, so that future maintainers know that the char order matters
ciphers/mixed_keyword_cypher.py
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Please do all this with 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.
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.
On your local machine, please run
|
It turned out that whoever has written this algorithm hard-coded the number of rows into it. |
ciphers/mixed_keyword_cypher.py
Outdated
alphabet = list(ascii_uppercase) | ||
alphabet_set = set(alphabet) |
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.
alphabet = list(ascii_uppercase) | |
alphabet_set = set(alphabet) | |
alphabet_set = set(ascii_uppercase) |
... plus other changes to remove references to alphabet
I think converting ascii_uppercase
to a list is unnecessary. Converting to a set, iterating, and indexing should all still work if you use the imported string directly.
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 can of course change that but I wanted to make it easier for people who would like to use other alphabets (e.g. cyrillic) for their ciphers. So it is your call - should I change it or not?
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.
If people want to use other alphabets such as Cyrillic, then they would have to go into the source code and change ascii_uppercase
to a different variable. That doesn't really address the issue that ascii_uppercase
(or whatever the user replaces it with) still doesn't need to be converted to a list.
If you want it to be easier to switch to a different alphabet, then perhaps you could have the function take in the alphabet string (ascii_uppercase
, etc) as an input parameter
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.
Fair point - I've done both - Now there's no conversion to the list and a different alphabet can be passed to the function. Thank you for the review!
The tests seem to be failing due to code outside of the file I've been editing. |
Other contributors have been experiencing the same issue with ruff... try running |
Running ruff resulted in conflicts with another branch - I've resolved them and then ran ruff again and nothing changed - ruff finds 4 errors but doesn't fix them automatically. |
* 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]>
@cclauss How is it that @rohan472000's changes in their own branch are making their way into this PR? |
for more information, see https://pre-commit.ci
@tianyizheng02 No worries, I'll update when #8743 gets merged |
@tianyizheng02 All done, thanks again! |
ciphers/mixed_keyword_cypher.py
Outdated
mapping[alphabet[letter_index]] = row[column] | ||
letter_index += 1 | ||
|
||
print(mapping) |
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.
Ideally the algorithm shouldn't have side effects such as printing, but I understand that printing the mapping is helpful for debugging purposes. Perhaps it'd be better to have a "verbose" function parameter that allows users to enable/disable printing the mapping
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.
Done - I've also added a test case that includes setting the verbose parameter to False
@tianyizheng02 Please approve this PR if you believe that it is ready to merge. |
Co-authored-by: Tianyi Zheng <[email protected]>
Thanks you for the edit yanvoi ! |
…#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]>
fixes: #8622
Describe your change:
I've tried my best with refactoring the code - it still might be too little but that's how much I've managed to do with the time I had.
Checklist:
Fixes: #{$ISSUE_NO}
.