Question: Python 3.x Decoder: Decoder.py # The input to the program is a text file # with the following format: # line 0: # lines 1

Python 3.x Decoder:

Python 3.x Decoder: Decoder.py # The input to the program is a

Decoder.py

# The input to the program is a text file # with the following format: # line 0: # lines 1 thru : # bits:'char' # lines +1 thru + + 1 import sys as sys # send the output to the console DEFAULT_OUTPUT_FILE = '' def main(): """  Purpose  The main program.  Usage: python3 Decoder.py  Sends output to DEFAULT_OUTPUT_FILE  Return:  :return: None  """  if len(sys.argv) != 2: print('Usage: python3', sys.argv[0], '') print('-- sends output to', DEFAULT_OUTPUT_FILE, '-- ') return s = read_file(sys.argv[1]) sizes = s[0].split() code_size = int(sizes[0]) message_size = int(sizes[1]) codes = s[1:code_size + 1] encoded = s[code_size + 1:code_size + message_size + 1] dc = build_decoder(codes) for enc in encoded: message = decode_message(enc, dc) print(message) def build_decoder(code_lines): """  Purpose:  Build the dictionary for decoding from the given list of code-lines  Preconditions:  :param code_lines: A list of strings of the form "CODE:''"  Return:  :return: a dictionary whose keys are 'CODE' and values are  """  codec = {} for code_str in code_lines: cchr = code_str.split(':') code = cchr[0] char = cchr[1] codec[code] = char[1] # The input file has ' ' around the character return codec def decode_message(coded_message, codec): """  Purpose:  Decode the message using the decoder.  Preconditions:  :param coded_message: An encoded string consisting of digits '0' and '1'  :param codec: a Dictionary of coded_message-character pairs  Return:  :return: A string decoded from coded_message  """  decoded_chars = [] first = 0 last = first + 1 while first """  Purpose:  Read the file with the given name.  Preconditions:  :param fname: a file name  Return:  :return: a list of strings consisting of the contents of the file  """  f = open(fname) lines = [l.rstrip() for l in f if l] f.close() return lines if __name__ == '__main__': main() 

Encoded files are on pastebin (There are three text files that I c/p) for convenience:

https://pastebin.com/mrFQpZum

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 Databases Questions!