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 base64
# method convertFileToBase64: Convert a file to its equivalent base64
def convertFileToBase64(self):
# 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 Base64(i.e., ASCII to base-64)
# I recommend calling the convertToBase64 method
pass # DELETE ONCE IMPLEMENTED
# method calculateFrequencies: Use self.Base64Text to calculate letter frequencies
def calculateFrequencies(self):
# TODO: Step through self.base64Text 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
# Base64 characters but all 64 are in the ASCII character set.
# If your Base64 character is a number (ASCII 48 to 57), then subtract 48 and you will have the correct index in self.frequencies - increment the frequency
# Do the same for uppercase (subtract 55 to get the index) and lowercase (subtract 61 to get the index).
# For '+','/' and '=' you may hardcode the indexes -62,63 and 64, respectively
# ERROR CHECKING: If a character is not Base-64 or '=' raise an InvalidBase64HuffmanError
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 build(self):
nodeList =[]
for oneChar in self.frequencies:
temp = BinaryTreeNode(oneChar)
nodeList.append(temp)
# [nodeList.append(BinaryTreeNode(oneMorseCode) for oneMorseCode in self.morseCodes]
while len(nodeList)>1:
node1= self.get_min(nodeList)
node2= self.get_min(nodeList)
updatedFreq = node1.getItem().getFrequency()+ node2.getItem().getFrequency()
# tempLetter = Letter("#", updatedFreq)
internalNode = BinaryTreeNode(Letter("#", updatedFreq))
internalNode.setLeft(node1)
internalNode.setRight(node2)
nodeList.append(internalNode)
self.root = nodeList[0]
#method get_min: GIVEN
#Used to build huffman tree by finding the node with the minimum frequency
def get_min(self, nodeList):
min =0
for i in range(len(nodeList)):
# print(nodeList[i].getItem().getFrequency())
if nodeList[i].getItem().getFrequency()< nodeList[min].getItem().getFrequency():
min = i
# [min = i for i in range(len(nodeList) if nodeList[i].getItem().getFrequency()< nodeList[min].getItem().getFrequency()]
result = nodeList[min]
nodeList.pop(min)
return result
#method getCodes: GIVEN
# method that returns a list of Code objects (i.e, 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 getCodes(self):
if self.root is None:
return None
else:
codes =[]
self.traverse(self.root.getLeft(),".", codes)
self.traverse(self.root.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 traverse(self, 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 Base64 character - add to "codes" list
# Be sure to reset prefix
pass # DELETE ONCE IMPLEMENTED
# method encoder: Encodes a Base64 string using the Huffman tree codes
def encoder(self, str):
# TODO: Retrieve the codes by calling getCodes()
# Encode str using the corresponding codes
# ERROR CHECKING: if str is length 0 or a code is not found raise InvalidBase64HuffmanError
encodedText =""
return encodedText
# method decoder: Decode a huffman code String of '.' & '-'. The result is a Base 64 string
def decoder(self, str):
# TODO: Complete the following method as follows
# - Begin at root
# - If current node is internal traverse left if str's character is '.'
# traverse right if str's character is '-'
# - If current node is not internal, it's a Base64 character so append to return string and reset to root
# ERROR CHECKING: If str is length 0
decodedText =""
return decodedText

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!