Add decoding script
This commit is contained in:
@@ -18,63 +18,88 @@ def find_node(nodes, target_character):
|
|||||||
# if desired character not found, return empty list
|
# if desired character not found, return empty list
|
||||||
return []
|
return []
|
||||||
|
|
||||||
# retrieve text to encode
|
def encode(txt):
|
||||||
txt = input("Text: ")
|
# create list of tuples in descending order of frequency: (character, frequency)
|
||||||
|
info = Counter(txt).most_common()
|
||||||
|
# change the list into ascending order
|
||||||
|
info.reverse()
|
||||||
|
# create list for character tuples
|
||||||
|
nodes = []
|
||||||
|
# create list for node usage frequencies
|
||||||
|
frequencies = []
|
||||||
|
|
||||||
# create list of tuples in descending order of frequency: (character, frequency)
|
# copy nodes and their usage frequencies to the dedicated lists
|
||||||
info = Counter(txt).most_common()
|
for item in info:
|
||||||
# change the list into ascending order
|
nodes.append(item[0])
|
||||||
info.reverse()
|
frequencies.append(item[1])
|
||||||
# create list for character tuples
|
|
||||||
nodes = []
|
|
||||||
# create list for node usage frequencies
|
|
||||||
frequencies = []
|
|
||||||
|
|
||||||
# copy nodes and their usage frequencies to the dedicated lists
|
# repeat until only one top-level node exists
|
||||||
for item in info:
|
while len(nodes) > 2:
|
||||||
nodes.append(item[0])
|
# combine two least frequent characters' nodes into a new tuple node, containing the old nodes (old_node_1, old_node_2)
|
||||||
frequencies.append(item[1])
|
new_node = (nodes[0], nodes[1])
|
||||||
|
# combine two least frequent characters' frequencies into a total frequency, to be used at the top level of the list of nodes
|
||||||
|
new_frequency = frequencies[0] + frequencies[1]
|
||||||
|
# remove nodes that have been nested inside the new node
|
||||||
|
del nodes[0:2]
|
||||||
|
# remove frequencies that have been summed and added to the new frequency
|
||||||
|
del frequencies[0:2]
|
||||||
|
|
||||||
# repeat until only one top-level node exists
|
# find index of last node with frequency below that of the new node
|
||||||
while len(nodes) > 2:
|
# if the largest frequency is smaller than the new one, place the new node at the end of the list
|
||||||
# combine two least frequent characters' nodes into a new tuple node, containing the old nodes (old_node_1, old_node_2)
|
if (frequencies[-1] < new_frequency):
|
||||||
new_node = (nodes[0], nodes[1])
|
i = -1
|
||||||
# combine two least frequent characters' frequencies into a total frequency, to be used at the top level of the list of nodes
|
else:
|
||||||
new_frequency = frequencies[0] + frequencies[1]
|
# else, loop over every frequency
|
||||||
# remove nodes that have been nested inside the new node
|
for index, item in enumerate(frequencies):
|
||||||
del nodes[0:2]
|
# if the frequency is greater than or equal to the new frequency
|
||||||
# remove frequencies that have been summed and added to the new frequency
|
if (item >= new_frequency):
|
||||||
del frequencies[0:2]
|
# record the index to insert the frequency at
|
||||||
|
i = index
|
||||||
|
# stop looping
|
||||||
|
break
|
||||||
|
|
||||||
# find index of last node with frequency below that of the new node
|
# insert the new node in its rightful position, maintaining ascending order of frequency
|
||||||
# if the largest frequency is smaller than the new one, place the new node at the end of the list
|
nodes.insert(i, new_node)
|
||||||
if (frequencies[-1] < new_frequency):
|
# insert the new frequency in its rightful position, maintaining ascending order of frequency
|
||||||
i = -1
|
frequencies.insert(i, new_frequency)
|
||||||
else:
|
|
||||||
# else, loop over every frequency
|
|
||||||
for index, item in enumerate(frequencies):
|
|
||||||
# if the frequency is greater than or equal to the new frequency
|
|
||||||
if (item >= new_frequency):
|
|
||||||
# record the index to insert the frequency at
|
|
||||||
i = index
|
|
||||||
# stop looping
|
|
||||||
break
|
|
||||||
|
|
||||||
# insert the new node in its rightful position, maintaining ascending order of frequency
|
# print all nodes
|
||||||
nodes.insert(i, new_node)
|
print("Nodes: %s" % nodes)
|
||||||
# insert the new frequency in its rightful position, maintaining ascending order of frequency
|
|
||||||
frequencies.insert(i, new_frequency)
|
|
||||||
|
|
||||||
# print all nodes
|
# encrypted text
|
||||||
print("Nodes: %s" % nodes)
|
output = ""
|
||||||
|
|
||||||
# encrypted text
|
# for every character in the text to be encrypted
|
||||||
output = ""
|
for char in txt:
|
||||||
|
# find its path and add it to the encrypted text
|
||||||
|
output += "".join(find_node(nodes, char))
|
||||||
|
|
||||||
# for every character in the text to be encrypted
|
# print encoded message
|
||||||
for char in txt:
|
print("Encoded message: %s" % output)
|
||||||
# find its path and add it to the encrypted text
|
|
||||||
output += "".join(find_node(nodes, char))
|
|
||||||
|
|
||||||
# print encrypted message
|
# return encoded message
|
||||||
print("Encrypted message: %s" % output)
|
return [output, nodes]
|
||||||
|
|
||||||
|
def decode(txt, nodes):
|
||||||
|
# string to hold decoded message
|
||||||
|
output = ""
|
||||||
|
# replicate nodes, for looping over and editing with the first part of the code
|
||||||
|
node = nodes
|
||||||
|
# for each digit
|
||||||
|
for digit in txt:
|
||||||
|
# get the node with the corresponding index
|
||||||
|
node = node[int(digit)]
|
||||||
|
# if the retrieved node isn't a tuple (i.e. it isn't a parent node)
|
||||||
|
if (not isinstance(node, tuple)):
|
||||||
|
# add the node's content to the output
|
||||||
|
output += node
|
||||||
|
# replace the retrieved node with the whole tree again, for looping over and editing with the next part of the code
|
||||||
|
node = nodes
|
||||||
|
|
||||||
|
# print decoded message
|
||||||
|
print("Decoded message: %s" % output)
|
||||||
|
# return decoded message
|
||||||
|
return output
|
||||||
|
|
||||||
|
encoding = encode(input("Text: "))
|
||||||
|
decode(encoding[0], encoding[1])
|
||||||
|
|||||||
Reference in New Issue
Block a user