Commented on (almost) every line

This commit is contained in:
JellieJayde
2024-04-29 12:38:28 -04:00
parent 24d1cdca55
commit a2844c1359

View File

@@ -24,60 +24,64 @@ from os import path
execDir = path.dirname(__file__) execDir = path.dirname(__file__)
def order(file): def order(file): # This is the main function that we will use to decrypt the code
dataLanguage = [] dataLanguage = [] # Variables:
amount = [] amount = [] # -
index = 0 index = 0 # -
sortedString = "" sortedString = "" # -
with open(execDir + file, "r") as f: with open(execDir + file, "r") as f: # With the readable file:
while True: # -------------------------------------------------------------------------------------------------------
fileChar = f.read(1).strip() while True: # Will loop from this line ^ to the next one, until told otherwise
fileChar = f.read(1).strip() # Reads the first character in the file
if fileChar.isalpha(): if fileChar.isalpha(): # If the character is in the alphabet (aka. a, b, c, etc...):
if fileChar.upper() not in dataLanguage: if fileChar.upper() not in dataLanguage: # If the character is not in the (currently) empty list:
dataLanguage.append(fileChar.upper()) dataLanguage.append(fileChar.upper()) # Put the character in the empty list
amount.append(1) amount.append(1) # Gives that letter an amount of 1, with the same index
else: else: # If the charatcer is in the alphabet, but is already in the list, then:
index = 0 index = 0 # Set the "index" variable to 0
while True: # ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
if dataLanguage[index] == fileChar.upper(): while True: # Loop from this ^ line to the next until told otherwise
amount[index] = amount[index] + 1 if dataLanguage[index] == fileChar.upper(): # If the letter with the index (which is currently zero) is the same as the current character:
break amount[index] = amount[index] + 1 # Add one to the amount of that charcater
break # Break the loop
else: else: # If the letter isn't the same as the current character:
index = index + 1 index = index + 1 # Add one to the index variable, then continue the loop
# ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
if fileChar == "~": # If the character is a tilde (I put a tilde at the end of the file so the program knows when the file is finished)
index = 0 # The index variable is now 0
Finished = False # Add a "Finished" variable
# ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
while not Finished: # If the "Finished" variable is false, it will loop from this line ^ to the next, until told other wise
Finished = True # Set's finished to true, but wont stop the loop because it just started the first cycle
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
for index in range(0, len(amount) - 1): # Will loop from this line ^ to the next for how many items are in the "amount" list
temp = amount[index] # Make a new variable, "temp" into a number in the amount list
tempLetter = dataLanguage[index] # Do the same thing, but with "tempLetter", and instead of a number, it's a ltter
if fileChar == "~": if temp < amount[index + 1]: # If the temperaroy number is less than the amount declared:
index = 0 amount[index] = amount[index + 1] # it will the replace the number declared in the "amount" list with the number ahead of it
Finished = False amount[index + 1] = temp # Will replace the number ahead of the amount declared, with the temperaroy number
while not Finished: dataLanguage[index] = dataLanguage[index + 1] # Replace the letter associated with the same index with the one ahead of it
Finished = True dataLanguage[index + 1] = tempLetter # Replace the letter ahead with the temperary letter
for index in range(0, len(amount) - 1): Finished = False # Make the "Finsihed" variable false, therefore resarting the loop
temp = amount[index] # * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
tempLetter = dataLanguage[index] if index == len(amount): # If the index is equal to the amount of numbers in the "amount" string
index = 0 # Reset the index
if temp < amount[index + 1]: # ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
amount[index] = amount[index + 1] for i in dataLanguage: # Will loop this next line for how many letters are in the "dataLanguage" list
amount[index + 1] = temp sortedString = sortedString + i # Add the letter it reads into a string
dataLanguage[index] = dataLanguage[index + 1]
dataLanguage[index + 1] = tempLetter
Finished = False
if index == len(amount):
index = 0
for i in dataLanguage:
sortedString = sortedString + i
return sortedString return sortedString # Returns the string
# ------------------------------------------------------------------------------------------------------------
# Then shows the data, proving it works
print (order("\\dataReference.txt"), "\n") print (order("\\dataReference.txt"), "\n")
print (order("\\dataEncoded19.ENC"), "\n") print (order("\\dataEncoded19.ENC"), "\n")