Add decoding script

This commit is contained in:
2020-02-23 10:12:22 +00:00
parent f01a096b9c
commit 82d4b04fbc

View File

@@ -18,25 +18,23 @@ 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
info.reverse()
# create list for character tuples
nodes = []
# create list for node usage frequencies
frequencies = []
# copy nodes and their usage frequencies to the dedicated lists
for item in info:
nodes.append(item[0]) nodes.append(item[0])
frequencies.append(item[1]) frequencies.append(item[1])
# repeat until only one top-level node exists # repeat until only one top-level node exists
while len(nodes) > 2: while len(nodes) > 2:
# combine two least frequent characters' nodes into a new tuple node, containing the old nodes (old_node_1, old_node_2) # combine two least frequent characters' nodes into a new tuple node, containing the old nodes (old_node_1, old_node_2)
new_node = (nodes[0], nodes[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 # combine two least frequent characters' frequencies into a total frequency, to be used at the top level of the list of nodes
@@ -65,16 +63,43 @@ while len(nodes) > 2:
# insert the new frequency in its rightful position, maintaining ascending order of frequency # insert the new frequency in its rightful position, maintaining ascending order of frequency
frequencies.insert(i, new_frequency) frequencies.insert(i, new_frequency)
# print all nodes # print all nodes
print("Nodes: %s" % nodes) print("Nodes: %s" % nodes)
# encrypted text # encrypted text
output = "" output = ""
# for every character in the text to be encrypted # for every character in the text to be encrypted
for char in txt: for char in txt:
# find its path and add it to the encrypted text # find its path and add it to the encrypted text
output += "".join(find_node(nodes, char)) output += "".join(find_node(nodes, char))
# print encrypted message # print encoded message
print("Encrypted message: %s" % output) print("Encoded message: %s" % output)
# return encoded message
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])