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