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:
32
main.py
32
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user