Fix node balancing
This commit is contained in:
@@ -37,6 +37,10 @@ def encode(txt):
|
|||||||
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
|
||||||
new_frequency = frequencies[0] + frequencies[1]
|
new_frequency = frequencies[0] + frequencies[1]
|
||||||
|
|
||||||
|
print(f"New node: {new_node}, new frequency: {new_frequency}")
|
||||||
|
print(f"Nodes to be removed: {nodes[0:2]}, frequencies to be removed: {frequencies[0:2]}")
|
||||||
|
|
||||||
# remove nodes that have been nested inside the new node
|
# remove nodes that have been nested inside the new node
|
||||||
del nodes[0:2]
|
del nodes[0:2]
|
||||||
# remove frequencies that have been summed and added to the new frequency
|
# remove frequencies that have been summed and added to the new frequency
|
||||||
@@ -46,11 +50,13 @@ def encode(txt):
|
|||||||
|
|
||||||
# if the largest frequency is smaller than the new one, place the new node at the end of the list
|
# if the largest frequency is smaller than the new one, place the new node at the end of the list
|
||||||
if (frequencies[-1] < new_frequency):
|
if (frequencies[-1] < new_frequency):
|
||||||
i = -1
|
print("i = -1")
|
||||||
|
i = len(frequencies)
|
||||||
else:
|
else:
|
||||||
# loop over every frequency
|
# loop over every frequency
|
||||||
for index, frequency in enumerate(frequencies):
|
for index, frequency in enumerate(frequencies):
|
||||||
if (frequency >= new_frequency):
|
if (frequency >= new_frequency):
|
||||||
|
print(f"{frequency} >= {new_frequency}. Breaking. Index is {index}")
|
||||||
# record the index to insert the frequency at
|
# record the index to insert the frequency at
|
||||||
i = index
|
i = index
|
||||||
break
|
break
|
||||||
@@ -58,18 +64,27 @@ def encode(txt):
|
|||||||
# insert the new node and frequency in their rightful positions, maintaining ascending order of frequency
|
# insert the new node and frequency in their rightful positions, maintaining ascending order of frequency
|
||||||
nodes.insert(i, new_node)
|
nodes.insert(i, new_node)
|
||||||
frequencies.insert(i, new_frequency)
|
frequencies.insert(i, new_frequency)
|
||||||
|
print(f"Nodes: {nodes}, frequencies: {frequencies}")
|
||||||
|
|
||||||
print(f"Nodes: {nodes}")
|
print(f"Nodes: {nodes}")
|
||||||
|
|
||||||
# encrypted text
|
# encrypted text
|
||||||
output = ""
|
output = ""
|
||||||
|
|
||||||
|
# characters and encoded values
|
||||||
|
encoded_values = {}
|
||||||
|
|
||||||
|
for char in set(txt):
|
||||||
|
# find the character's path and add it to the dictionary
|
||||||
|
encoded_values[char] = "".join(find_node(nodes, char))
|
||||||
|
|
||||||
|
print(f"Encoded values: {encoded_values}")
|
||||||
|
|
||||||
for char in txt:
|
for char in txt:
|
||||||
# find its path and add it to the encrypted text
|
# add text to final output
|
||||||
output += "".join(find_node(nodes, char))
|
output += encoded_values[char]
|
||||||
|
|
||||||
print(f"Encoded message: {output}")
|
print(f"Encoded message: {output}")
|
||||||
|
|
||||||
return [output, nodes]
|
return [output, nodes]
|
||||||
|
|
||||||
def decode(txt, nodes):
|
def decode(txt, nodes):
|
||||||
@@ -90,4 +105,4 @@ def decode(txt, nodes):
|
|||||||
return output
|
return output
|
||||||
|
|
||||||
encoding = encode(input("Text: "))
|
encoding = encode(input("Text: "))
|
||||||
decode(encoding[0], encoding[1])
|
decode(encoding[0], encoding[1])
|
||||||
Reference in New Issue
Block a user