In python. Please follow the skeleton code provided. Please don't use a code that was given in a previous answer, it doe
Posted: Sun May 15, 2022 10:08 am
In python. Please follow the skeleton code provided. Please
don't use a code that was given in a previous answer, it doesn't
really complete the task at hand and the format is kind of odd.
thank you.
Text Files:
face.text:
11100000111
11011111011
10110101101
01111111110
01111111110
10101110101
11010001011
11101110111
11110001111
flag.txt:
ABBBBBBBBBBBB
AABBBBBBBBBBB
AAACCCCCCCCCC
AAAACCCCCCCCC
AAAABBBBBBBBB
AAABBBBBBBBBB
AACCCCCCCCCCC
ACCCCCCCCCCCC
Codes:
rle_compress.py:
def get_lines(input_file_name):
'''
Open the file, and read all its lines into a list.
Then, return the list
'''
# Replace this line:
return ['aaa\n','aabb\n']
def rle_encode(lines, out_file_name):
'''
Compress the lines, using run-length encoding.
Open the given file for output.
For each line,
- trim the trailing \n
- find runs of repeated chars
- if a run is longer than 9, finish it and start a new run.
- write() each run to the file, as two chars
- remember the last run in the line!
- at the end of each line, write() a '\n'
Close the file
'''
# Replace this line:
pass
def main():
in_filename = input('Input file? ')
out_filename = input('Output file? ')
lines = get_lines(in_filename)
rle_encode(lines, out_filename)
if __name__ == '__main__':
main()
rle_expand.py:
from rle_compress import get_lines
def rle_decode(lines, out_filename):
'''
This is similar to rle_encode, in that you should
- open the given file for output, and
- process the given list of lines, while
- writing values to the output file.
However, each line now consists of pairs of chars,
where each pair is a run.
so you need something like this, to grab pairs:
for i in range(0, len(line), 2):
count = line # <-- this is a character, not an int!
value = line[i + 1]
'''
pass
def main():
in_filename = input('Input file? ')
out_filename = input('Output file? ')
lines = get_lines(in_filename)
rle_decode(lines, out_filename)
if __name__ == '__main__':
main()
Run-length encoding Faxes are sent over low-bandwidth phone lines. To speed up transmission, their data is compressed. One step in this compression is run-length encoding. Run-length encoding works well in data which has repeated values. For example, the sequence of values: 5 5 5 5 2 2 1 2 2 2 0 0 0 0 has 14 values, but could be written as 'four 5s, then two 2s, then one 1, then three 2s, then four Os', or 4 5 2 2 1 1 3 2 4 0 (just 10 values). Thus we have compressed the data, using 4 fewer values. Your Tasks 1. Encoding: write a program called rle_compress.py, which does run-length encoding. The program should work as follows: use input to ask the user for the name of an input file. open the input file for reading, and read all its lines (You must implement read_lines). close the input file. use input to ask the user for the name of an output file. open the output file for writing.
for each line that you have read: strip off the trailing newline: line-line.rstrip("\n\r') for each char in the line compare each character against the previous one. if they're the same, increase the run length. if they're different,write() two chars to the output (length and value) don't forget about the final (length value) pair! at the end of each line, write a "\n" newline character. close the output file IMPORTANT: Each run length should fit in a single character (1 through 9). So if you get a run longer than 9, you should start a new run. Example: if the input file has this content: AAABCCCCCCCCCCCCD AAABBBBBBBCCCAAAAA then your output file should have this content: 3A1B9C2C2D 3A7B3C5A (Notice that 11 C values are compressed into 9C2C). Save your program in a file called rle_compress.py 2. Decoding implement another program, called rle_expand.py. It is similar to the previous program: It should ask the user for an input file name, and an output file name. Then it should read from the input file, and write values to the output file. However, unlike the rle_compress.py, the new program rle_expand.py should process two numbers at a time (a run length, and a value). For each pair, it should write one or more values. 3. Check that both programs have been implemented correctly, by first compressing a file, then expanding it, and see if you get the original values back: a) Run rle_compress.py, with input face.txt, and output face_rle.txt. b) Run rle_expand.py, with input face_rle.txt, and output face_2.txt. c) Both face.txt and face_2.txt should have the same data.
don't use a code that was given in a previous answer, it doesn't
really complete the task at hand and the format is kind of odd.
thank you.
Text Files:
face.text:
11100000111
11011111011
10110101101
01111111110
01111111110
10101110101
11010001011
11101110111
11110001111
flag.txt:
ABBBBBBBBBBBB
AABBBBBBBBBBB
AAACCCCCCCCCC
AAAACCCCCCCCC
AAAABBBBBBBBB
AAABBBBBBBBBB
AACCCCCCCCCCC
ACCCCCCCCCCCC
Codes:
rle_compress.py:
def get_lines(input_file_name):
'''
Open the file, and read all its lines into a list.
Then, return the list
'''
# Replace this line:
return ['aaa\n','aabb\n']
def rle_encode(lines, out_file_name):
'''
Compress the lines, using run-length encoding.
Open the given file for output.
For each line,
- trim the trailing \n
- find runs of repeated chars
- if a run is longer than 9, finish it and start a new run.
- write() each run to the file, as two chars
- remember the last run in the line!
- at the end of each line, write() a '\n'
Close the file
'''
# Replace this line:
pass
def main():
in_filename = input('Input file? ')
out_filename = input('Output file? ')
lines = get_lines(in_filename)
rle_encode(lines, out_filename)
if __name__ == '__main__':
main()
rle_expand.py:
from rle_compress import get_lines
def rle_decode(lines, out_filename):
'''
This is similar to rle_encode, in that you should
- open the given file for output, and
- process the given list of lines, while
- writing values to the output file.
However, each line now consists of pairs of chars,
where each pair is a run.
so you need something like this, to grab pairs:
for i in range(0, len(line), 2):
count = line # <-- this is a character, not an int!
value = line[i + 1]
'''
pass
def main():
in_filename = input('Input file? ')
out_filename = input('Output file? ')
lines = get_lines(in_filename)
rle_decode(lines, out_filename)
if __name__ == '__main__':
main()
Run-length encoding Faxes are sent over low-bandwidth phone lines. To speed up transmission, their data is compressed. One step in this compression is run-length encoding. Run-length encoding works well in data which has repeated values. For example, the sequence of values: 5 5 5 5 2 2 1 2 2 2 0 0 0 0 has 14 values, but could be written as 'four 5s, then two 2s, then one 1, then three 2s, then four Os', or 4 5 2 2 1 1 3 2 4 0 (just 10 values). Thus we have compressed the data, using 4 fewer values. Your Tasks 1. Encoding: write a program called rle_compress.py, which does run-length encoding. The program should work as follows: use input to ask the user for the name of an input file. open the input file for reading, and read all its lines (You must implement read_lines). close the input file. use input to ask the user for the name of an output file. open the output file for writing.
for each line that you have read: strip off the trailing newline: line-line.rstrip("\n\r') for each char in the line compare each character against the previous one. if they're the same, increase the run length. if they're different,write() two chars to the output (length and value) don't forget about the final (length value) pair! at the end of each line, write a "\n" newline character. close the output file IMPORTANT: Each run length should fit in a single character (1 through 9). So if you get a run longer than 9, you should start a new run. Example: if the input file has this content: AAABCCCCCCCCCCCCD AAABBBBBBBCCCAAAAA then your output file should have this content: 3A1B9C2C2D 3A7B3C5A (Notice that 11 C values are compressed into 9C2C). Save your program in a file called rle_compress.py 2. Decoding implement another program, called rle_expand.py. It is similar to the previous program: It should ask the user for an input file name, and an output file name. Then it should read from the input file, and write values to the output file. However, unlike the rle_compress.py, the new program rle_expand.py should process two numbers at a time (a run length, and a value). For each pair, it should write one or more values. 3. Check that both programs have been implemented correctly, by first compressing a file, then expanding it, and see if you get the original values back: a) Run rle_compress.py, with input face.txt, and output face_rle.txt. b) Run rle_expand.py, with input face_rle.txt, and output face_2.txt. c) Both face.txt and face_2.txt should have the same data.