From a30e010c5c603fe0d99ebb14d70f2d74c48b7388 Mon Sep 17 00:00:00 2001 From: Kenn Kitchen Date: Mon, 27 Oct 2025 09:03:24 -0400 Subject: [PATCH] Replaced encode and decode with excode. Replaced encode and decode with a single function, excode, that accepts a parameter for encode/decode and handles both actions. --- .python-version | 1 + main.py | 32 +++++++++++--------------------- pyproject.toml | 7 +++++++ uv.lock | 8 ++++++++ 4 files changed, 27 insertions(+), 21 deletions(-) create mode 100644 .python-version create mode 100644 pyproject.toml create mode 100644 uv.lock diff --git a/.python-version b/.python-version new file mode 100644 index 0000000..24ee5b1 --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +3.13 diff --git a/main.py b/main.py index 3971ff3..ca69aed 100644 --- a/main.py +++ b/main.py @@ -47,13 +47,13 @@ def main(): if sys.argv[2] == "e": print("Encoding!") for iteration in range(0, iterations): - iteration_result = encode(seed_values[iteration], sys.argv[1]) + iteration_result = excode(True, seed_values[iteration], sys.argv[1]) print("Result:", "\"" + iteration_result + "\"") elif sys.argv[2] == "d": print("Decoding!") for iteration in range(0, iterations): - iteration_result = decode(seed_values[iteration], sys.argv[1]) + iteration_result = excode(False, seed_values[iteration], sys.argv[1]) print("Result:", "\"" + iteration_result + "\"") else: @@ -61,30 +61,20 @@ def main(): sys.exit(1) -def encode(offset: int, message: str) -> str: +def excode(encoding: bool, offset: int, message: str) -> str: result_message: str = "" for index_a in range(len(message)): for index_b in range(len(alpha_base)): if message[index_a] == alpha_base[index_b]: - location = index_b + offset - if location > (len(alpha_base) - 1): - location = location - (len(alpha_base) - 1) - result_message = result_message + alpha_base[location] - break - - return result_message - - -def decode(offset: int, message: str) -> str: - result_message: str = "" - - for index_a in range(len(message)): - for index_b in range(len(alpha_base)): - if message[index_a] == alpha_base[index_b]: - location = index_b - offset - if location < 0: - location = location + (len(alpha_base) - 1) + if encoding: + location = index_b + offset + if location > (len(alpha_base) - 1): + location = location - (len(alpha_base) - 1) + else: + location = index_b - offset + if location < 0: + location = location + (len(alpha_base) - 1) result_message = result_message + alpha_base[location] break diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..36cc8bc --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,7 @@ +[project] +name = "enigmamachine" +version = "0.1.0" +description = "Add your description here" +readme = "README.md" +requires-python = ">=3.13" +dependencies = [] diff --git a/uv.lock b/uv.lock new file mode 100644 index 0000000..b1e0597 --- /dev/null +++ b/uv.lock @@ -0,0 +1,8 @@ +version = 1 +revision = 3 +requires-python = ">=3.13" + +[[package]] +name = "enigmamachine" +version = "0.1.0" +source = { virtual = "." }