mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 17:34:49 +08:00
Fix sphinx/build_docs warnings for ciphers (#12485)
* Fix sphinx/build_docs warnings for ciphers * Fix
This commit is contained in:
@ -7,24 +7,29 @@ def encrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
|
||||
"""
|
||||
encrypt
|
||||
=======
|
||||
|
||||
Encodes a given string with the caesar cipher and returns the encoded
|
||||
message
|
||||
|
||||
Parameters:
|
||||
-----------
|
||||
* input_string: the plain-text that needs to be encoded
|
||||
* key: the number of letters to shift the message by
|
||||
|
||||
* `input_string`: the plain-text that needs to be encoded
|
||||
* `key`: the number of letters to shift the message by
|
||||
|
||||
Optional:
|
||||
* alphabet (None): the alphabet used to encode the cipher, if not
|
||||
|
||||
* `alphabet` (``None``): the alphabet used to encode the cipher, if not
|
||||
specified, the standard english alphabet with upper and lowercase
|
||||
letters is used
|
||||
|
||||
Returns:
|
||||
|
||||
* A string containing the encoded cipher-text
|
||||
|
||||
More on the caesar cipher
|
||||
=========================
|
||||
|
||||
The caesar cipher is named after Julius Caesar who used it when sending
|
||||
secret military messages to his troops. This is a simple substitution cipher
|
||||
where every character in the plain-text is shifted by a certain number known
|
||||
@ -32,26 +37,28 @@ def encrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
|
||||
|
||||
Example:
|
||||
Say we have the following message:
|
||||
"Hello, captain"
|
||||
``Hello, captain``
|
||||
|
||||
And our alphabet is made up of lower and uppercase letters:
|
||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
``abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ``
|
||||
|
||||
And our shift is "2"
|
||||
And our shift is ``2``
|
||||
|
||||
We can then encode the message, one letter at a time. "H" would become "J",
|
||||
since "J" is two letters away, and so on. If the shift is ever two large, or
|
||||
We can then encode the message, one letter at a time. ``H`` would become ``J``,
|
||||
since ``J`` is two letters away, and so on. If the shift is ever two large, or
|
||||
our letter is at the end of the alphabet, we just start at the beginning
|
||||
("Z" would shift to "a" then "b" and so on).
|
||||
(``Z`` would shift to ``a`` then ``b`` and so on).
|
||||
|
||||
Our final message would be "Jgnnq, ecrvckp"
|
||||
Our final message would be ``Jgnnq, ecrvckp``
|
||||
|
||||
Further reading
|
||||
===============
|
||||
|
||||
* https://en.m.wikipedia.org/wiki/Caesar_cipher
|
||||
|
||||
Doctests
|
||||
========
|
||||
|
||||
>>> encrypt('The quick brown fox jumps over the lazy dog', 8)
|
||||
'bpm yCqks jzwEv nwF rCuxA wDmz Bpm tiHG lwo'
|
||||
|
||||
@ -85,23 +92,28 @@ def decrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
|
||||
"""
|
||||
decrypt
|
||||
=======
|
||||
|
||||
Decodes a given string of cipher-text and returns the decoded plain-text
|
||||
|
||||
Parameters:
|
||||
-----------
|
||||
* input_string: the cipher-text that needs to be decoded
|
||||
* key: the number of letters to shift the message backwards by to decode
|
||||
|
||||
* `input_string`: the cipher-text that needs to be decoded
|
||||
* `key`: the number of letters to shift the message backwards by to decode
|
||||
|
||||
Optional:
|
||||
* alphabet (None): the alphabet used to decode the cipher, if not
|
||||
|
||||
* `alphabet` (``None``): the alphabet used to decode the cipher, if not
|
||||
specified, the standard english alphabet with upper and lowercase
|
||||
letters is used
|
||||
|
||||
Returns:
|
||||
|
||||
* A string containing the decoded plain-text
|
||||
|
||||
More on the caesar cipher
|
||||
=========================
|
||||
|
||||
The caesar cipher is named after Julius Caesar who used it when sending
|
||||
secret military messages to his troops. This is a simple substitution cipher
|
||||
where very character in the plain-text is shifted by a certain number known
|
||||
@ -110,27 +122,29 @@ def decrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
|
||||
|
||||
Example:
|
||||
Say we have the following cipher-text:
|
||||
"Jgnnq, ecrvckp"
|
||||
``Jgnnq, ecrvckp``
|
||||
|
||||
And our alphabet is made up of lower and uppercase letters:
|
||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
``abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ``
|
||||
|
||||
And our shift is "2"
|
||||
And our shift is ``2``
|
||||
|
||||
To decode the message, we would do the same thing as encoding, but in
|
||||
reverse. The first letter, "J" would become "H" (remember: we are decoding)
|
||||
because "H" is two letters in reverse (to the left) of "J". We would
|
||||
continue doing this. A letter like "a" would shift back to the end of
|
||||
the alphabet, and would become "Z" or "Y" and so on.
|
||||
reverse. The first letter, ``J`` would become ``H`` (remember: we are decoding)
|
||||
because ``H`` is two letters in reverse (to the left) of ``J``. We would
|
||||
continue doing this. A letter like ``a`` would shift back to the end of
|
||||
the alphabet, and would become ``Z`` or ``Y`` and so on.
|
||||
|
||||
Our final message would be "Hello, captain"
|
||||
Our final message would be ``Hello, captain``
|
||||
|
||||
Further reading
|
||||
===============
|
||||
|
||||
* https://en.m.wikipedia.org/wiki/Caesar_cipher
|
||||
|
||||
Doctests
|
||||
========
|
||||
|
||||
>>> decrypt('bpm yCqks jzwEv nwF rCuxA wDmz Bpm tiHG lwo', 8)
|
||||
'The quick brown fox jumps over the lazy dog'
|
||||
|
||||
@ -150,41 +164,44 @@ def brute_force(input_string: str, alphabet: str | None = None) -> dict[int, str
|
||||
"""
|
||||
brute_force
|
||||
===========
|
||||
|
||||
Returns all the possible combinations of keys and the decoded strings in the
|
||||
form of a dictionary
|
||||
|
||||
Parameters:
|
||||
-----------
|
||||
* input_string: the cipher-text that needs to be used during brute-force
|
||||
|
||||
* `input_string`: the cipher-text that needs to be used during brute-force
|
||||
|
||||
Optional:
|
||||
* alphabet: (None): the alphabet used to decode the cipher, if not
|
||||
|
||||
* `alphabet` (``None``): the alphabet used to decode the cipher, if not
|
||||
specified, the standard english alphabet with upper and lowercase
|
||||
letters is used
|
||||
|
||||
More about brute force
|
||||
======================
|
||||
|
||||
Brute force is when a person intercepts a message or password, not knowing
|
||||
the key and tries every single combination. This is easy with the caesar
|
||||
cipher since there are only all the letters in the alphabet. The more
|
||||
complex the cipher, the larger amount of time it will take to do brute force
|
||||
|
||||
Ex:
|
||||
Say we have a 5 letter alphabet (abcde), for simplicity and we intercepted the
|
||||
following message:
|
||||
|
||||
"dbc"
|
||||
|
||||
Say we have a ``5`` letter alphabet (``abcde``), for simplicity and we intercepted
|
||||
the following message: ``dbc``,
|
||||
we could then just write out every combination:
|
||||
ecd... and so on, until we reach a combination that makes sense:
|
||||
"cab"
|
||||
``ecd``... and so on, until we reach a combination that makes sense:
|
||||
``cab``
|
||||
|
||||
Further reading
|
||||
===============
|
||||
|
||||
* https://en.wikipedia.org/wiki/Brute_force
|
||||
|
||||
Doctests
|
||||
========
|
||||
|
||||
>>> brute_force("jFyuMy xIH'N vLONy zILwy Gy!")[20]
|
||||
"Please don't brute force me!"
|
||||
|
||||
|
Reference in New Issue
Block a user