DEV Community

Discussion on: Daily Coding Puzzles - Nov 4th - Nov 9th

Collapse
 
gypsydave5 profile image
David Wickes

Common Lisp

(defun last-word (word)
  (let ((cs (coerce word 'list)))
    (coerce (reduce #'(lambda (word c)
                        (if (char> (first word) c)
                            (append word (list c))
                            (cons c word)))
                    (rest cs)
                    :initial-value (list (first cs)))
            'string)))

Quick little test...

(mapcar #'last-word (list "CAB"
                          "JAM"
                          "CODE"
                          "ABAAB"
                          "CABCBBABC"
                          "ABCABCABC"
                          "ZXCASDQWE"))
;; => ("CAB" "MJA" "OCDE" "BBAAA" "CCCABBBAB" "CCCBAABAB" "ZXCASDQWE")