Last active
August 29, 2015 14:24
-
-
Save shortsightedsid/1e8ce7cab931b1ee2d2e to your computer and use it in GitHub Desktop.
Garland Word length calculator in Common Lisp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment