Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Python 3.x Rewriting file so it's more efficient I have code that works, and but it's not super efficient I don't know how to make
Python 3.x Rewriting file so it's more efficient
I have code that works, and but it's not super efficient I don't know how to make it so it is not storing all the files data at the same time:
Decoder.Py:
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-message.txt:
33 2 001:'e' 00000000000000000000000000000000:'W' 00000001:'l' 00000000000000000000000000000001:'O' 0000001:'u' 0000000000000000000000000000001:'c' 0000000001:'i' 0000000000000000000000000001:'d' 000001:'t' 000000000001:'y' 000000000000000000000000000001:'2' 00001:'h' 00000000000000000000000000001:'B' 000000001:'a' 0000000000000000001:'"' 000000000000000001:'.' 00000000000000000000001:'z' 000000000000000000000000001:'v' 01:'s' 00000000001:'o' 0000000000000000000001:''' 0000000000000000000000001:';' 000000000000000000000001:'3' 00000000000000000000000001:'b' 000000000000001:'I' 1:' ' 00000000000000001:',' 000000000000000000001:'w' 0000000000000001:'g' 00000000000001:'r' 00000000000000000001:'k' 0001:'n' 0000000000001:'m' 00000000000000000000000000000001000000100000000000001101000000000000000000000000000000100001000000000010000000000100000001100001000000001000000000000000000000000000110000000011010000000010000000000010000000001000100000000000000010000000000000000000000001 0000000000000000001000000000000000000000000000000000000100100011010000000000100000000000010010000010000100000000010001000000000000000110100000000000010010000000100000001010000000000000000110000000001000001000000000000000000000101100000010100000010000000010000000100000001000000000001100000100001001100000000000000000000000000001000000100000100000000000000000000001000000000000000001000000000000000000110000000000000010001100000100001001100000000000000000000000000000100000000000000000000000110000000000010010000000010000000000000101100000000000000100000000000000000000000000100110000000000000000000100010000000000100000000000000000000100011000010000000001000000000000100000000000000001100000000010000010110000001010000001000000001000000010000000100000000000110000000000000000000000000100100100011000001000000000000010000001001000000000000000001
Question 2 (15 points) Purpose: To practice the skill of reading and modifying someone else's code. To recognize that efficiency of memory use is sometimes as important as computational efficiency Degree of Difficulty: Moderate On the Assignment 10 Moodle page, you'll find the program Decoder.py. It is a fully functional program that is able to decode files that have the expected file format (as outlined in Question 1 The program follows a simple design, andis well- documented. All the pieces shouldbe famiiar to you, as they are similar to discussion in class. It contains 4 functions, as follows: main) : The main program. 1. Reads the entire file 2. Extracts the code 3. Extracts the encoded message 4. Builds the codec 5. Decodes the message build.decoder(code_lines) Build the dictionary for decoding starting with the given list of code-Lines. decode_message(coded mes sage, codec) Decode the message using the decoder. read file(fname) : Reads every line in the named file, putting all the Lines into a single list. However, there is a serious efficiency problem with this program. The design requires that the entire file be read at once, and that all the data get passed from one function to another. For example, the function read file() reads the entire file's contents, and stores it all in a single list of strings. From there, the lines of encoded text are decoded one at a time, but all the encoded text is stored in a list. For small files, this is okay, since modern computers are fairly big. However, for very large files, bigger than what we're working with, it is very inefficient to store the whole file's worth of data in memory all at once. It is better, when it is possible, avoid this situation b is to take the given code, and rewrite parts of it, so that the program does not store all the data in the file all at once. This task will require some study of the current code, and some redesign. You do not need to write new algorithms. You just need to arrange it so that the program is not storing all file's data at the same time. For this question, Version Control may be especially useful, asyouwill want to save your program at various stages of development while you rewrite it. Whether you actually use the history of your development or not, ensuring that you could is a professional attitude A small collection of example files are also provided to you to test your program. Phoenix has included some his thoughts that have been encoded. Submit the answer to Phoenikmystery as a10q2-mystery.txt
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started