mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-18 09:10:16 +00:00
c909da9b08
* pre-commit: Upgrade psf/black for stable style 2023 Updating https://github.com/psf/black ... updating 22.12.0 -> 23.1.0 for their `2023 stable style`. * https://github.com/psf/black/blob/main/CHANGES.md#2310 > This is the first [psf/black] release of 2023, and following our stability policy, it comes with a number of improvements to our stable style… Also, add https://github.com/tox-dev/pyproject-fmt and https://github.com/abravalheri/validate-pyproject to pre-commit. I only modified `.pre-commit-config.yaml` and all other files were modified by pre-commit.ci and psf/black. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
105 lines
3.0 KiB
Python
105 lines
3.0 KiB
Python
import itertools
|
|
import string
|
|
from collections.abc import Generator, Iterable
|
|
|
|
|
|
def chunker(seq: Iterable[str], size: int) -> Generator[tuple[str, ...], None, None]:
|
|
it = iter(seq)
|
|
while True:
|
|
chunk = tuple(itertools.islice(it, size))
|
|
if not chunk:
|
|
return
|
|
yield chunk
|
|
|
|
|
|
def prepare_input(dirty: str) -> str:
|
|
"""
|
|
Prepare the plaintext by up-casing it
|
|
and separating repeated letters with X's
|
|
"""
|
|
|
|
dirty = "".join([c.upper() for c in dirty if c in string.ascii_letters])
|
|
clean = ""
|
|
|
|
if len(dirty) < 2:
|
|
return dirty
|
|
|
|
for i in range(len(dirty) - 1):
|
|
clean += dirty[i]
|
|
|
|
if dirty[i] == dirty[i + 1]:
|
|
clean += "X"
|
|
|
|
clean += dirty[-1]
|
|
|
|
if len(clean) & 1:
|
|
clean += "X"
|
|
|
|
return clean
|
|
|
|
|
|
def generate_table(key: str) -> list[str]:
|
|
# I and J are used interchangeably to allow
|
|
# us to use a 5x5 table (25 letters)
|
|
alphabet = "ABCDEFGHIKLMNOPQRSTUVWXYZ"
|
|
# we're using a list instead of a '2d' array because it makes the math
|
|
# for setting up the table and doing the actual encoding/decoding simpler
|
|
table = []
|
|
|
|
# copy key chars into the table if they are in `alphabet` ignoring duplicates
|
|
for char in key.upper():
|
|
if char not in table and char in alphabet:
|
|
table.append(char)
|
|
|
|
# fill the rest of the table in with the remaining alphabet chars
|
|
for char in alphabet:
|
|
if char not in table:
|
|
table.append(char)
|
|
|
|
return table
|
|
|
|
|
|
def encode(plaintext: str, key: str) -> str:
|
|
table = generate_table(key)
|
|
plaintext = prepare_input(plaintext)
|
|
ciphertext = ""
|
|
|
|
# https://en.wikipedia.org/wiki/Playfair_cipher#Description
|
|
for char1, char2 in chunker(plaintext, 2):
|
|
row1, col1 = divmod(table.index(char1), 5)
|
|
row2, col2 = divmod(table.index(char2), 5)
|
|
|
|
if row1 == row2:
|
|
ciphertext += table[row1 * 5 + (col1 + 1) % 5]
|
|
ciphertext += table[row2 * 5 + (col2 + 1) % 5]
|
|
elif col1 == col2:
|
|
ciphertext += table[((row1 + 1) % 5) * 5 + col1]
|
|
ciphertext += table[((row2 + 1) % 5) * 5 + col2]
|
|
else: # rectangle
|
|
ciphertext += table[row1 * 5 + col2]
|
|
ciphertext += table[row2 * 5 + col1]
|
|
|
|
return ciphertext
|
|
|
|
|
|
def decode(ciphertext: str, key: str) -> str:
|
|
table = generate_table(key)
|
|
plaintext = ""
|
|
|
|
# https://en.wikipedia.org/wiki/Playfair_cipher#Description
|
|
for char1, char2 in chunker(ciphertext, 2):
|
|
row1, col1 = divmod(table.index(char1), 5)
|
|
row2, col2 = divmod(table.index(char2), 5)
|
|
|
|
if row1 == row2:
|
|
plaintext += table[row1 * 5 + (col1 - 1) % 5]
|
|
plaintext += table[row2 * 5 + (col2 - 1) % 5]
|
|
elif col1 == col2:
|
|
plaintext += table[((row1 - 1) % 5) * 5 + col1]
|
|
plaintext += table[((row2 - 1) % 5) * 5 + col2]
|
|
else: # rectangle
|
|
plaintext += table[row1 * 5 + col2]
|
|
plaintext += table[row2 * 5 + col1]
|
|
|
|
return plaintext
|