Skip to content

Add autoclave cipher - #8029

Merged
cclauss merged 11 commits into
TheAlgorithms:masterfrom
VictorRS27:master
Dec 18, 2022
Merged

Add autoclave cipher#8029
cclauss merged 11 commits into
TheAlgorithms:masterfrom
VictorRS27:master

Conversation

@VictorRS27

Copy link
Copy Markdown
Contributor

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

  • 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}.

@CaedenPH CaedenPH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please link some documentation at the start of the file and include more doctests within each function

Comment thread ciphers/autoclave.py Outdated
Comment on lines +6 to +12
@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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Comment thread ciphers/autoclave.py Outdated
Comment on lines +21 to +23
if plaintext == "":
raise ValueError("plaintext is empty")
if key == "":

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
if plaintext == "":
raise ValueError("plaintext is empty")
if key == "":
if not plaintext:
raise ValueError("plaintext is empty")
if not key:

Comment thread ciphers/autoclave.py Outdated
if key == "":
raise ValueError("key is empty")

key = key + plaintext

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
key = key + plaintext
key += plaintext

Comment thread ciphers/autoclave.py Outdated
@@ -0,0 +1,111 @@
def encrypt(plaintext, key) -> str:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
def encrypt(plaintext, key) -> str:
def encrypt(plaintext, key) -> str:

These parameters need to be typehinted

Comment thread ciphers/autoclave.py Outdated
Comment on lines +102 to +111
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")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please move this into an if __name__ == "__main__": block

Comment thread ciphers/autoclave.py Outdated
@@ -0,0 +1,111 @@
def encrypt(plaintext, key) -> str:

@cclauss cclauss Dec 15, 2022

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Clave is Latin for key so the more popular name for this algorithm is autokey.

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

Comment thread ciphers/autoclave.py Outdated
>>> encrypt("hello world", "coffee")
'jsqqs avvwo'
"""
if type(plaintext) != str:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

PEP8: Use isinstance(plaintext, str) instead of directly comparing types.

@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Dec 18, 2022
@algorithms-keeper algorithms-keeper Bot added the tests are failing Do not merge until tests pass label Dec 18, 2022
@algorithms-keeper algorithms-keeper Bot removed the awaiting reviews This PR is ready to be reviewed label Dec 18, 2022
@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Dec 18, 2022
@algorithms-keeper algorithms-keeper Bot removed the tests are failing Do not merge until tests pass label Dec 18, 2022
@cclauss
cclauss merged commit 3f8b2af into TheAlgorithms:master Dec 18, 2022
@algorithms-keeper algorithms-keeper Bot removed the awaiting reviews This PR is ready to be reviewed label Dec 18, 2022
Cjkjvfnby pushed a commit to Cjkjvfnby/Python that referenced this pull request Mar 13, 2023
* 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>
@cclauss

cclauss commented Sep 6, 2026

Copy link
Copy Markdown
Member

@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:

@priya-sundaram-dev

Copy link
Copy Markdown
Contributor

Done — opened #15208. It re-enables the four ML examples plus neural_network/perceptron.py.

The reason they wouldn't come back as-is: they'd bit-rotted against scikit-learn. plot_confusion_matrix and the Boston dataset were both removed in scikit-learn 1.2, so --doctest-modules couldn't even import the files. I swapped in ConfusionMatrixDisplay.from_estimator and load_diabetes, dropped the artificial time.sleep()s, and for the perceptron I seeded a local RNG + capped the training loop so it can't spin forever on non-separable data. All doctests + ruff pass locally.

cclauss pushed a commit that referenced this pull request Sep 6, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants