Streamline searching for index of new frequency in list

This commit is contained in:
2020-02-23 09:34:52 +00:00
parent 99ffc4d8c2
commit 891c0c86ed

View File

@@ -44,18 +44,21 @@ while len(nodes) > 1:
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
del frequencies[0:2] del frequencies[0:2]
# find index of last node with frequency below that of the new node # find index of last node with frequency below that of the new node
i = 0 # if the largest frequency is smaller than the new one, place the new node at the end of the list
# if there are more nodes to compare to if (frequencies[-1] < new_frequency):
if (len(nodes) > 0): i = -1
# if the largest frequency is smaller than the new one, place the new node at the end of the list else:
if (frequencies[-1] < new_frequency): # else, loop over every frequency
i = -1 for index, item in enumerate(frequencies):
# else, if the first frequency is larger than the new one # if the frequency is greater than or equal to the new frequency
elif not (frequencies[i] >= new_frequency): if (item >= new_frequency):
# find the largest frequency that is smaller than the new one # record the index to insert the frequency at
while (frequencies[i] < new_frequency): i = index
i += 1 # stop looping
break
# insert the new node in its rightful position, maintaining ascending order of frequency # insert the new node in its rightful position, maintaining ascending order of frequency
nodes.insert(i, new_node) nodes.insert(i, new_node)
# 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