Skip to content

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

Merged
merged 17 commits into from
Jun 9, 2023
Merged

Improve readability of ciphers/mixed_keyword_cypher.py #8626

merged 17 commits into from
Jun 9, 2023

Conversation

yanvoi
Copy link
Contributor

@yanvoi yanvoi commented Apr 8, 2023

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.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@algorithms-keeper algorithms-keeper bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files tests are failing Do not merge until tests pass labels Apr 8, 2023
Comment on lines 27 to 30
unique_chars = []
for char in keyword:
if char not in unique_chars:
unique_chars.append(char)
Copy link
Member

@cclauss cclauss Apr 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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.

Copy link
Contributor

@tianyizheng02 tianyizheng02 Jun 8, 2023

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

s = []
for _ in range(len_temp):
s.append(temp[k])
modified_alphabet = []
Copy link
Member

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

Copy link
Contributor Author

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.

@cclauss cclauss changed the title fixes: #8622 Improve readability of ciphers/mixed_keyword_cypher.py Apr 9, 2023
@cclauss
Copy link
Member

cclauss commented Apr 9, 2023

On your local machine, please run python3 -m doctest -v ciphers/mixed_keyword_cypher.py
because pytest is failing on:

=================================== FAILURES ===================================
_____________ [doctest] ciphers.mixed_keyword_cypher.mixed_keyword _____________
008     H E L O
009     A B C D
010     F G I J
011     K M N P
012     Q R S T
013     U V W X
014     Y Z
015     and map vertically
016 
017     >>> mixed_keyword("college", "UNIVERSITY")  # doctest: +NORMALIZE_WHITESPACE
Expected:
    {'A': 'C', 'B': 'A', 'C': 'I', 'D': 'P', 'E': 'U', 'F': 'Z', 'G': 'O', 'H': 'B',
     'I': 'J', 'J': 'Q', 'K': 'V', 'L': 'L', 'M': 'D', 'N': 'K', 'O': 'R', 'P': 'W',
     'Q': 'E', 'R': 'F', 'S': 'M', 'T': 'S', 'U': 'X', 'V': 'G', 'W': 'H', 'X': 'N',
     'Y': 'T', 'Z': 'Y'}
    'XKJGUFMJST'
Got:
    'XKJGUFMJST'

@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Apr 9, 2023
@yanvoi yanvoi requested a review from cclauss April 9, 2023 10:07
@yanvoi
Copy link
Contributor Author

yanvoi commented Apr 10, 2023

It turned out that whoever has written this algorithm hard-coded the number of rows into it.

Comment on lines 26 to 27
alphabet = list(ascii_uppercase)
alphabet_set = set(alphabet)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Contributor Author

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?

Copy link
Contributor

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

Copy link
Contributor Author

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!

@yanvoi yanvoi requested a review from tianyizheng02 May 10, 2023 13:00
@tianyizheng02 tianyizheng02 mentioned this pull request May 11, 2023
14 tasks
@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label May 12, 2023
@yanvoi
Copy link
Contributor Author

yanvoi commented May 12, 2023

The tests seem to be failing due to code outside of the file I've been editing.

@tianyizheng02
Copy link
Contributor

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 ruff check --fix . to see if ruff can automatically fix any of the linting issues

@yanvoi
Copy link
Contributor Author

yanvoi commented May 12, 2023

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.

@yanvoi yanvoi requested a review from tianyizheng02 May 12, 2023 13:09
@tianyizheng02
Copy link
Contributor

@yanvoi I believe the ruff errors were fixed in #8732, so the ruff check should pass after you update your branch

* 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]>
@tianyizheng02
Copy link
Contributor

@cclauss How is it that @rohan472000's changes in their own branch are making their way into this PR?

@tianyizheng02
Copy link
Contributor

Oh my goddddd I knew this was gonna happen...

@yanvoi The ruff error is once again in another file that you didn't edit, but I already fixed it in #8743. Once that PR gets merged, updating your branch on GitHub should finally fix the issue

@yanvoi
Copy link
Contributor Author

yanvoi commented May 17, 2023

@tianyizheng02 No worries, I'll update when #8743 gets merged

@tianyizheng02
Copy link
Contributor

@yanvoi The ruff error was fixed in #8746 instead, so you should be good to update your branch

@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label May 22, 2023
@yanvoi
Copy link
Contributor Author

yanvoi commented May 22, 2023

@tianyizheng02 All done, thanks again!

mapping[alphabet[letter_index]] = row[column]
letter_index += 1

print(mapping)
Copy link
Contributor

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

Copy link
Contributor Author

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

@algorithms-keeper algorithms-keeper bot added tests are failing Do not merge until tests pass and removed tests are failing Do not merge until tests pass labels Jun 8, 2023
@yanvoi yanvoi requested a review from tianyizheng02 June 8, 2023 06:27
@cclauss
Copy link
Member

cclauss commented Jun 8, 2023

@tianyizheng02 Please approve this PR if you believe that it is ready to merge.

@yanvoi yanvoi requested a review from tianyizheng02 June 9, 2023 07:32
@cclauss cclauss enabled auto-merge (squash) June 9, 2023 09:06
@cclauss cclauss merged commit 9c9da8e into TheAlgorithms:master Jun 9, 2023
@Starmania
Copy link

Thanks you for the edit yanvoi !

sedatguzelsemme pushed a commit to sedatguzelsemme/Python that referenced this pull request Sep 15, 2024
…#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]>
@isidroas isidroas mentioned this pull request Jan 25, 2025
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor code so it's readable
5 participants