Improve readability of ciphers/mixed_keyword_cypher.py#8626
Improve readability of ciphers/mixed_keyword_cypher.py#8626cclauss merged 17 commits intoTheAlgorithms:masterfrom yanvoi:issue8622fix
Conversation
| unique_chars = [] | ||
| for char in keyword: | ||
| if char not in unique_chars: | ||
| unique_chars.append(char) |
There was a problem hiding this comment.
Please make this a set.
There was a problem hiding this comment.
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.
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.
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.
| 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.
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.
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.
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 <cclauss@me.com>
* 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 <cclauss@me.com>
|
@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.
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.
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 <tianyizheng02@gmail.com>
|
Thanks you for the edit yanvoi ! |
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}.