Add autoclave cipher - #8029
Conversation
CaedenPH
left a comment
There was a problem hiding this comment.
Please link some documentation at the start of the file and include more doctests within each function
| @params | ||
| plaintext - a normal text to be encrypted (string) | ||
| key - a small text or word to start the replacing (sFtring) | ||
|
|
||
| @return | ||
| A string with the ciphertext | ||
|
|
There was a problem hiding this comment.
| @params | |
| plaintext - a normal text to be encrypted (string) | |
| key - a small text or word to start the replacing (sFtring) | |
| @return | |
| A string with the ciphertext |
Typically, we want the parameter names to be self-documenting so this isn't really necessary
| if plaintext == "": | ||
| raise ValueError("plaintext is empty") | ||
| if key == "": |
There was a problem hiding this comment.
| if plaintext == "": | |
| raise ValueError("plaintext is empty") | |
| if key == "": | |
| if not plaintext: | |
| raise ValueError("plaintext is empty") | |
| if not key: |
| if key == "": | ||
| raise ValueError("key is empty") | ||
|
|
||
| key = key + plaintext |
There was a problem hiding this comment.
| key = key + plaintext | |
| key += plaintext |
| @@ -0,0 +1,111 @@ | |||
| def encrypt(plaintext, key) -> str: | |||
There was a problem hiding this comment.
| def encrypt(plaintext, key) -> str: | |
| def encrypt(plaintext, key) -> str: |
These parameters need to be typehinted
| operation = int(input("Type 1 to encrypt or 2 to decrypt:")) | ||
| if operation == 1: | ||
| plaintext = str(input("Typeplaintext to be encrypted:\n")) | ||
| key = str(input("Type the key:\n")) | ||
| print(encrypt(plaintext, key)) | ||
| elif operation == 2: | ||
| ciphertext = str(input("Type the ciphertext to be decrypted:\n")) | ||
| key = str(input("Type the key:\n")) | ||
| print(decrypt(ciphertext, key)) | ||
| decrypt("jsqqs avvwo", "coffee") |
There was a problem hiding this comment.
please move this into an if __name__ == "__main__": block
| @@ -0,0 +1,111 @@ | |||
| def encrypt(plaintext, key) -> str: | |||
There was a problem hiding this comment.
Clave is Latin for key so the more popular name for this algorithm is autokey.
| def encrypt(plaintext, key) -> str: | |
| ””” | |
| https://en.wikipedia.org/wiki/Autokey_cipher | |
| > An autokey cipher (also known as the autoclave cipher) is a cipher that incorporates the message (the plaintext into the key. The key is generated from the message in some automated fashion, sometimes by selecting certain letters from the text or, more commonly, by adding a short primer key to the front of the message. | |
| ””” | |
| def encrypt(plaintext: str, key: str) -> str: |
| >>> encrypt("hello world", "coffee") | ||
| 'jsqqs avvwo' | ||
| """ | ||
| if type(plaintext) != str: |
There was a problem hiding this comment.
PEP8: Use isinstance(plaintext, str) instead of directly comparing types.
for more information, see https://pre-commit.ci
* Add autoclave cipher * Update autoclave with the given suggestions * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fixing errors * Another fixes * Update and rename autoclave.py to autokey.py * Rename gaussian_naive_bayes.py to gaussian_naive_bayes.py.broken.txt * Rename gradient_boosting_regressor.py to gradient_boosting_regressor.py.broken.txt * Rename random_forest_classifier.py to random_forest_classifier.py.broken.txt * Rename random_forest_regressor.py to random_forest_regressor.py.broken.txt * Rename equal_loudness_filter.py to equal_loudness_filter.py.broken.txt Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
|
@priya-sundaram-dev Several years ago, when I merged this pull request, I disabled five unrelated algorithms. I have no idea why I disabled them, but perhaps they were slowing down our CI tests. Can you please create a pull request that re-enables them and also re-enables neural_network/perceptron.py.DISABLED, as discussed in: |
|
Done — opened #15208. It re-enables the four ML examples plus The reason they wouldn't come back as-is: they'd bit-rotted against scikit-learn. |
Re-enable the four scikit-learn machine-learning examples and the
neural-network perceptron that had been disabled (renamed to
.broken.txt / .DISABLED), and modernize them so they import and run
cleanly on current scikit-learn and pass the doctest CI:
machine_learning/gaussian_naive_bayes.py
machine_learning/random_forest_classifier.py
- Replace the removed sklearn.metrics.plot_confusion_matrix with
ConfusionMatrixDisplay.from_estimator (removed in scikit-learn 1.2).
- Drop the artificial time.sleep() calls.
machine_learning/gradient_boosting_regressor.py
machine_learning/random_forest_regressor.py
- Replace the removed load_boston dataset (removed in scikit-learn
1.2 for ethical reasons) with the bundled load_diabetes dataset so
the examples run offline.
- Avoid an unused-variable lint (RUF059).
neural_network/perceptron.py
- Use a dedicated seeded random.Random instance instead of the global
random state, so training is reproducible and thread-safe under the
parallel test runner.
- Cap training at epoch_number epochs so it always terminates even on
non-linearly-separable data (previously an unbounded while True).
- Have training() and sort() return their results instead of printing,
per the contribution guidelines, and update the doctests accordingly.
Requested by @cclauss in #8029; perceptron follow-up to #15206.
Describe your change:
I'm adding the autoclave or autokey cipher.
Is similar to the vigenerè cipher, but using the own plaintext as the key.
The code implements an encrypt and a decrypt function.
For more info:
https://en.wikipedia.org/wiki/Autokey_cipher
Checklist:
Fixes: #{$ISSUE_NO}.