;; See https://www.reddit.com/r/dailyprogrammer/comments/3d4fwj/20150713_challenge_223_easy_garland_words/ ;; ;; A Garland word is one that starts and ends with the same N letters in the same order, ;; for some N greater than 0, but less than the length of the word. ;; Eg. onion -> 2 ;; ceramic -> 1 ;; programmer -> 0 ;; alfalfa -> 4 (defun garland (str) "Calculate the garland word length of a string" (loop with len = (length str) for i from 1 below len when (string= (subseq str 0 i) (subseq str (- len i) len)) maximize i)) (defun garland-file (filename) "Get the string with a largest garland length in a file. The file has 1 word per line" (with-open-file (stream filename :direction :input) (loop for string = (read-line stream nil :eof) with max-garland = 0 with garland-string = "" until (eq string :eof) do (let ((current-garland (garland string))) (when (> current-garland max-garland) (setf max-garland current-garland) (setf garland-string string))) finally (return (values garland-string max-garland)))))