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.
This commit is contained in:
Kenn Kitchen
2025-10-27 09:03:24 -04:00
parent bc49b72c9e
commit a30e010c5c
4 changed files with 27 additions and 21 deletions

1
.python-version Normal file
View File

@@ -0,0 +1 @@
3.13

32
main.py
View File

@@ -47,13 +47,13 @@ def main():
if sys.argv[2] == "e": if sys.argv[2] == "e":
print("Encoding!") print("Encoding!")
for iteration in range(0, iterations): 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 + "\"") print("Result:", "\"" + iteration_result + "\"")
elif sys.argv[2] == "d": elif sys.argv[2] == "d":
print("Decoding!") print("Decoding!")
for iteration in range(0, iterations): 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 + "\"") print("Result:", "\"" + iteration_result + "\"")
else: else:
@@ -61,30 +61,20 @@ def main():
sys.exit(1) sys.exit(1)
def encode(offset: int, message: str) -> str: def excode(encoding: bool, offset: int, message: str) -> str:
result_message: str = "" result_message: str = ""
for index_a in range(len(message)): for index_a in range(len(message)):
for index_b in range(len(alpha_base)): for index_b in range(len(alpha_base)):
if message[index_a] == alpha_base[index_b]: if message[index_a] == alpha_base[index_b]:
location = index_b + offset if encoding:
if location > (len(alpha_base) - 1): location = index_b + offset
location = location - (len(alpha_base) - 1) if location > (len(alpha_base) - 1):
result_message = result_message + alpha_base[location] location = location - (len(alpha_base) - 1)
break else:
location = index_b - offset
return result_message if location < 0:
location = location + (len(alpha_base) - 1)
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)
result_message = result_message + alpha_base[location] result_message = result_message + alpha_base[location]
break break

7
pyproject.toml Normal file
View File

@@ -0,0 +1,7 @@
[project]
name = "enigmamachine"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.13"
dependencies = []

8
uv.lock generated Normal file
View File

@@ -0,0 +1,8 @@
version = 1
revision = 3
requires-python = ">=3.13"
[[package]]
name = "enigmamachine"
version = "0.1.0"
source = { virtual = "." }