You are viewing a single comment's thread. Return to all comments →
Racket
(define (group items [grouped null]) (cond [(null? items) (reverse grouped)] [(null? grouped) (group (rest items) (list (list (first items))))] [else (match-define (list* g gs) grouped) (match-define (list* x xs) items) (group xs (if (equal? x (first g)) (cons (cons x g) gs) (cons (list x) grouped)))])) (define (compress chars) (define l (length chars)) (define s (string (first chars))) (if (= l 1) s (string-append s (number->string l)))) (define uncompressed (read-line)) (printf "~a\n" (for/fold ([encoded ""]) ([c (group (string->list uncompressed))]) (string-append encoded (compress c))))
Seems like cookies are disabled on this browser, please enable them to open this website
String Compression
You are viewing a single comment's thread. Return to all comments →
Racket