August 19, 2024

P13 - Run-length encoding of a list (direct solution)

Implement the so-called run-length encoding data compression method directly. I.e. don't explicitly create the sublists containing the duplicates, as in problem P09, but only count them. As in problem P11, simplify the result list by replacing the singleton lists (1 X) by X.

Example:* (encode-direct '(a a a a b c c a a d e e e e))((4 A) B (2 C) (2 A) D (4 E))

lisp

(defun encode-direct (lista)
  (if (null lista)
      ()
    (add-item (car lista) (encode-direct (cdr lista)))
    )
  )

(defun add-item (item code)
  (if (null code)
      (list item)
    (append (add-to-run item (car code))
	    (cdr code))
    )
  )

(defun add-to-run (item run)
  (if (listp run)
      (if (equal item (second run))
	  (list (cons (1+ (car run)) (cdr run)))
	(list item run))
    (if (equal item run)
	(list (list 2 item))
      (list item run)
      )
    )
  )
Be first to comment
Leave a reply