Can now handle user-specified iterations. Uses a file for the seeds.
This commit is contained in:
72
main.py
72
main.py
@@ -2,7 +2,7 @@ import random
|
||||
import sys
|
||||
import time
|
||||
|
||||
alphaBase = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,. !="
|
||||
alpha_base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,. !="
|
||||
|
||||
def main():
|
||||
### Argument validation ###
|
||||
@@ -10,7 +10,7 @@ def main():
|
||||
print("Usage: uv run main.py [0-58] [0-58] [0-58] '[message]' 'e|d' [int]")
|
||||
sys.exit(1)
|
||||
|
||||
base_max = len(alphaBase)
|
||||
base_max = len(alpha_base)
|
||||
|
||||
if not is_number(sys.argv[1]) or not is_number(sys.argv[2]) or not is_number(sys.argv[3]):
|
||||
print("Invalid seed value.")
|
||||
@@ -41,72 +41,72 @@ def main():
|
||||
iterations = int(sys.argv[6])
|
||||
|
||||
### Processing ###
|
||||
# current_timestamp = time.time()
|
||||
# print(current_timestamp)
|
||||
|
||||
seed_values = []
|
||||
|
||||
if sys.argv[5] == "e":
|
||||
for iteration in range(0, iterations):
|
||||
random.seed(time.time())
|
||||
seed_values.append(random.randint(1, base_max))
|
||||
with open("seeds.txt", "w") as f:
|
||||
for iteration in range(0, iterations):
|
||||
random.seed(time.time())
|
||||
seed_values.append(random.randint(1, base_max))
|
||||
f.write(str(seed_values[iteration]) + "\n")
|
||||
f.close()
|
||||
|
||||
if sys.argv[5] == "d":
|
||||
seed_values.append(seed1)
|
||||
seed_values.append(seed2)
|
||||
seed_values.append(seed3)
|
||||
with open("seeds.txt", "r") as f:
|
||||
for iteration in range(0, iterations):
|
||||
seed = f.readline()
|
||||
seed_values.append(int(seed))
|
||||
f.close()
|
||||
|
||||
iteration_result = ""
|
||||
|
||||
if sys.argv[5] == "e":
|
||||
print("Encoding!")
|
||||
for iteration in range(0, iterations):
|
||||
iteration_result = encode(seed_values[iteration], sys.argv[4])
|
||||
print("Seed:", iteration, "is", seed_values[iteration])
|
||||
# print("Seed:", iteration, "is", seed_values[iteration])
|
||||
|
||||
print("Result:", iteration_result)
|
||||
print("Result:", "\"" + iteration_result + "\"")
|
||||
elif sys.argv[5] == "d":
|
||||
print("Decoding!")
|
||||
for iteration in range(0, iterations):
|
||||
iteration_result = decode(seed_values[iteration], sys.argv[4])
|
||||
|
||||
print("Seeds used:", seed1, seed2, seed3)
|
||||
print("Result:", iteration_result)
|
||||
# print("Seeds used:", seed1, seed2, seed3)
|
||||
print("Result:", "\"" + iteration_result + "\"")
|
||||
else:
|
||||
print("Invalid option!")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def encode(offset, message):
|
||||
print("Encoding!")
|
||||
result_message = ""
|
||||
|
||||
resultMessage = ""
|
||||
|
||||
for indexA in range(len(message)):
|
||||
for indexB in range(len(alphaBase)):
|
||||
if message[indexA] == alphaBase[indexB]:
|
||||
location = indexB + offset
|
||||
if location > (len(alphaBase) - 1):
|
||||
location = location - (len(alphaBase) - 1)
|
||||
resultMessage = resultMessage + alphaBase[location]
|
||||
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 resultMessage
|
||||
return result_message
|
||||
|
||||
|
||||
def decode(offset, message):
|
||||
print("Decoding!")
|
||||
result_message = ""
|
||||
|
||||
resultMessage = ""
|
||||
|
||||
for indexA in range(len(message)):
|
||||
for indexB in range(len(alphaBase)):
|
||||
if message[indexA] == alphaBase[indexB]:
|
||||
location = indexB - offset
|
||||
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(alphaBase) - 1)
|
||||
resultMessage = resultMessage + alphaBase[location]
|
||||
location = location + (len(alpha_base) - 1)
|
||||
result_message = result_message + alpha_base[location]
|
||||
break
|
||||
|
||||
return resultMessage
|
||||
return result_message
|
||||
|
||||
|
||||
def is_number(i):
|
||||
|
||||
Reference in New Issue
Block a user