Skip to content

added doctest to playfair_cipher.py #10823

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
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Oct 22, 2023
commit 9d3ccb30433123fb2df753393d682a6ce64d9e6b
15 changes: 8 additions & 7 deletions ciphers/playfair_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
3) It is a multiple letter encryption cipher

The implementation in the code below encodes alphabets only.
It removes spaces, special characters and numbers from the
It removes spaces, special characters and numbers from the
code.

Playfair is no longer used by military forces because of known
insecurities and of the advent of automated encryption devices.
Playfair is no longer used by military forces because of known
insecurities and of the advent of automated encryption devices.
This cipher is regarded as insecure since before World War I.
"""

Expand Down Expand Up @@ -81,7 +81,6 @@ def generate_table(key: str) -> list[str]:


def encode(plaintext: str, key: str) -> str:

"""encode function encodes the given plaintext by taking
two strings, the plaintext and the key, as input
and returns an encoded string
Expand Down Expand Up @@ -122,7 +121,7 @@ def encode(plaintext: str, key: str) -> str:
def decode(ciphertext: str, key: str) -> str:
"""The decode function decodes the input string
using the provided key and returns a decoded string

>>> print(decode("BMZFAZRZDH", "HAZARD"))
FIREHAZARD
>>> print(decode("HNBWBPQT", "AUTOMOBILE"))
Expand Down Expand Up @@ -150,9 +149,11 @@ def decode(ciphertext: str, key: str) -> str:

return plaintext


if __name__ == "__main__":
import doctest

doctest.testmod()

print("Encoded:",encode("BYE AND THANKS", "GREETING"))
print("Decoded:",decode("CXRBANRLBALQ", "GREETING"))
print("Encoded:", encode("BYE AND THANKS", "GREETING"))
print("Decoded:", decode("CXRBANRLBALQ", "GREETING"))