Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part II: File Compressor (20 points) Write a function compress.fileO, that takes

ID: 3712342 • Letter: P

Question

Part II: File Compressor (20 points) Write a function compress.fileO, that takes one argument, filename, which is the name of the file you need to compress In this part, you should read the file line by line, compress each line as described in Part I, and append each compressed line into a list. In the end, you should return the result list that consists of all the compressed lines, in the correct order. You should call the function you wrote in Part I to compress each individual line for you. CSE 101 - Spring 2018 Lab #1 1 Page 2 Examples: Due to very long outputs, please consult the lab driver for examples Note: If you use MacOS, you may find the format getting messed up when you open one of the test files. Try to open the .txt files in Pycharm itself or some kind of text editor like Atom, and it should look correct

Explanation / Answer

Please find the Python Code for compressing the contents of a file passed as input parametre.

import re
import os
from ast import literal_eval

def compress_file(fileName):
with open(fileName, 'rb') as file_in:
p = re.compile(r'[w]+|[W]')
split = p.findall(file_in.read())

word_list, b = []
for word in split:
try:
r = word_list.index(word) + 1
except ValueError:
word_list.append(word)
r = len(word_list)
b.append(r)

with open('compressed.txt', 'r+') as file_out:
file_out.write('{} {}'.format(str(word_list), str(b)))

Please let me know in case of any clarifications required. Thanks!