Mini Quiz 3 Solutions
Solutions: You can find the file with solutions for all questions here.
This "Mini-Quiz" is completely optional. Do as much or as little as you want.
To begin the quiz, use the following ok command:
python3 ok -u
Question 1
Run-length encoding is a very simple data compression technique, whereby runs of data are compressed and stored as a single value. A run is defined to be a contiguous sequence of the same number. For example, in the (finite) sequence
1, 1, 1, 1, 1, 6, 6, 6, 6, 2, 5, 5, 5
there are four runs: one each of 1, 6, 2, and 5. We can represent the same sequence as a sequence of two-element lists:
(1 5), (6 4), (2 1), (5 3)
Notice that the first element of each list is the number of times a particular number appears in a run, and the second element is the number in the run.
We will extend this idea to (possibly infinite) streams. Write a
function called rle
that takes in a stream of data, and returns a
corresponding stream of two-element lists, which represents the run-length
encoded version of the stream. You do not have to consider compressing
infinite runs.
(define (rle s)
(define (track-run elem st len)
(cond ((null? st) (cons-stream (list elem len) nil))
((= elem (car st)) (track-run elem (cdr-stream st) (+ len 1)))
(else (cons-stream (list elem len) (rle st))))
)
(if (null? s)
nil
(track-run (car s) (cdr-stream s) 1)))