Question: Please fill in the TODO sections and explain how you did them. import base 6 4 # method convertFileToBase 6 4 : Convert a file
Please fill in the TODO sections and explain how you did them.
import base
# method convertFileToBase: Convert a file to its equivalent base
def convertFileToBaseself:
# TODO: Use self.filename to read in each line of the file. Be sure to include the newline character for each line
# Now, convert the text to Baseie ASCII to base
# I recommend calling the convertToBase method
pass # DELETE ONCE IMPLEMENTED
# method calculateFrequencies: Use self.BaseText to calculate letter frequencies
def calculateFrequenciesself:
# TODO: Step through self.baseText and increment the frequency of each letter by calling the setFrequency method
# Note that setFrequency is a modified setter in that you pass no arguments. Look at setFrequency in the Letter class when
# this method is invoked, it increments the frequency by one.
# ALSO, as in the case of initializeFrequencies, you should take advantage of the ASCII values yes the string is
# Base characters but all are in the ASCII character set.
# If your Base character is a number ASCII to then subtract and you will have the correct index in self.frequencies increment the frequency
# Do the same for uppercase subtract to get the index and lowercase subtract to get the index
# For and you may hardcode the indexes and respectively
# ERROR CHECKING: If a character is not Base or raise an InvalidBaseHuffmanError
pass # DELETE ONCE IMPLEMENTED
# method build: GIVEN
# Builds huffman tree using frequencies in self.frequencies list
# Sets the root data field of the Binary Tree
def buildself:
nodeList
for oneChar in self.frequencies:
temp BinaryTreeNodeoneChar
nodeList.appendtemp
# nodeListappendBinaryTreeNodeoneMorseCode for oneMorseCode in self.morseCodes
while lennodeList:
node self.getminnodeList
node self.getminnodeList
updatedFreq nodegetItemgetFrequency nodegetItemgetFrequency
# tempLetter Letter# updatedFreq
internalNode BinaryTreeNodeLetter# updatedFreq
internalNode.setLeftnode
internalNode.setRightnode
nodeList.appendinternalNode
self.root nodeList
#method getmin: GIVEN
#Used to build huffman tree by finding the node with the minimum frequency
def getminself nodeList:
min
for i in rangelennodeList:
# printnodeListigetItemgetFrequency
if nodeListigetItemgetFrequency nodeListmingetItemgetFrequency:
min i
# min i for i in rangelennodeList if nodeListigetItemgetFrequency nodeListmingetItemgetFrequency
result nodeListmin
nodeList.popmin
return result
#method getCodes: GIVEN
# method that returns a list of Code objects ie the character and its huffman code
# In the tree, left edges are designated as and right edges as
#
# DO NOT CHANGE THIS METHOD, but you need to write the "traverse" method.
def getCodesself:
if self.root is None:
return None
else:
codes
self.traverseselfroot.getLeft codes
self.traverseselfroot.getRight codes
return codes
# method traverse: recursive method to traverse the Huffman tree. For each leaf node,
# add a record to the "codes" list that this method accepts.
def traverseself node, prefix, codes:
# TODO: Fill in this method
# If node is internal, recursively traverse left for and right for
# Be sure to add the or to the "prefix"
# If node is NOT internal, it is a Base character add to "codes" list
# Be sure to reset prefix
pass # DELETE ONCE IMPLEMENTED
# method encoder: Encodes a Base string using the Huffman tree codes
def encoderself str:
# TODO: Retrieve the codes by calling getCodes
# Encode str using the corresponding codes
# ERROR CHECKING: if str is length or a code is not found raise InvalidBaseHuffmanError
encodedText
return encodedText
# method decoder: Decode a huffman code String of & The result is a Base string
def decoderself str:
# TODO: Complete the following method as follows
# Begin at root
# If current node is internal traverse left if strs character is
# traverse right if strs character is
# If current node is not internal, it's a Base character so append to return string and reset to root
# ERROR CHECKING: If str is length
decodedText
return decodedText
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
