One part of the standard used to send black-and-white faxes involves run-length encoding. This is a way to use fewer values to represent a stream of data which has lots of duplicate 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
0s', or 4, 5, 2, 5, 1, 1, 3, 2, 4, 0
(just 10
values). Thus we have compressed the data, using 4 fewer
values.
Write a program rle.py
which does run-length
encoding.
The program should work as follows:
int
value.
write
two numbers (length and value) to the
output file.
write
is less powerful
than print
, you'll have to include a
blank between output numbers.
Save your program in a file called rle_compress.py
Implement another program,
called rle_expand.py
. It should do the opposite of
the previous program.
In other words, it should ask the user for input and output file names. Then it should read from the input file, and write to the output file.
Unlike the rle_compress.py
, the new
program rle_expand.py
should process two numbers at a
time (a run length, and a value), and should write one or more
values to the output file (separated by spaces, of course).
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.
mycourses.unh.edu
, find
cs414, and locate assignment 7. Click the "Submit" button, and
upload both
rle_compress.py
and
rle_expand.py
.