Run Length Encoding
February 26, 2010
The compress
function is tricky only because the output is delayed at least one character from the input. That means we need an initialization pass through the main loop to “prime the pump” and start the recursion. Put-run
is a separate function because it is called in two places:
(define (compress in-port out-port)
(define (n->char n) (integer->char (+ 64 n)))
(define (show-run prev n)
(display #\~ out-port)
(display (n->char n) out-port)
(display prev out-port))
(define (put-run prev n)
(cond ((char=? prev #\~) (show-run #\~ n))
((< n 4) (display (make-string n prev) out-port))
((< n 27) (show-run prev n))
(else (show-run prev 26) (put-run prev (- n 26)))))
(let loop ((c (read-char in-port)) (prev #f) (n 0))
(cond ((eof-object? c) (if prev (put-run prev n)))
((and prev (char=? c prev))
(loop (read-char in-port) prev (+ n 1)))
(prev (put-run prev n) (loop (read-char in-port) c 1))
(else (loop (read-char in-port) c 1)))))
Expansion is simpler than compression; just read the input, handle tildes specially, and output everything else:
(define (expand in-port out-port)
(define (char->n c) (- (char->integer c) 64))
(let loop ((c (read-char in-port)))
(unless (eof-object? c)
(if (char=? c #\~)
(let* ((n (char->n (read-char in-port)))
(c (read-char in-port)))
(display (make-string n c) out-port))
(display c out-port))
(loop (read-char in-port)))))
Here is an example:
> (with-input-from-string "ABBB~CDDDDDEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
(lambda () (compress (current-input-port) (current-output-port))))
ABBB~A~C~ED~ZE~DE
> (with-input-from-string "ABBB~A~C~ED~ZE~DE"
(lambda () (expand (current-input-port) (current-output-port))))
ABBB~CDDDDDEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
You can see the program at http://programmingpraxis.codepad.org/ZQoT0BnW.
[…] Praxis – Run Length Encoding By Remco Niemeijer In today’s Programming Praxis exercise we have to implement a run length encoding algorithm. The provided […]
My Haskell solution (see http://bonsaicode.wordpress.com/2010/02/26/programming-praxis-run-length-encoding/ for a version with comments):
My take on compress. I interpreted “any literal appearance of the tilde in the input is encoded as a run of length 1” as being a special case, so the output is different from Niemeijer’s version when there are several tildes in a row.
Full code with a quickcheck test: http://codepad.org/VakQUriW
Python, done using regular expressions.
Like domor, I separately encoded each ~ in the input.