Created
March 22, 2011 01:45
-
-
Save daviddavis/880608 to your computer and use it in GitHub Desktop.
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
(ns palindromes | |
(:require [clojure.string :as str])) | |
(def text | |
(str "I'll just type in some example text here and embed a little | |
palindrome - A man, a plan, a canal, Panama! - I expect that will be | |
the longest palindrome found in this text. | |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. | |
Integer volutpat lorem imperdiet ante bibendum ullamcorper. Mauris | |
tempor hendrerit justo at elementum. Vivamus elit magna, accumsan id | |
condimentum a, luctus a ipsum. Donec fermentum, lectus at posuere | |
ullamcorper, mauris lectus tincidunt nulla, ut placerat justo odio sed | |
odio. Nulla blandit lorem sit amet odio varius nec vestibulum ante | |
ornare. Aliquam feugiat, velit a rhoncus rutrum, turpis metus pretium | |
dolor, et mattis leo turpis non est. Sed aliquet, sapien quis | |
consequat condimentum, sem magna ornare ligula, id blandit odio nisl | |
vitae erat. Nam vulputate tincidunt quam, non lacinia risus tincidunt | |
lacinia. Aenean fermentum tristique porttitor. Nam id dolor a eros | |
accumsan imperdiet. Aliquam quis nibh et dui ultricies cursus. Nunc | |
et ante non sapien vehicula rutrum. Duis posuere dictum blandit. Nunc | |
vitae tempus purus.") | |
) | |
(defn sanitize [n] | |
(str/replace n #"[^a-z]" "") | |
) | |
(defn palindromep [n] | |
(= (seq (sanitize n)) (reverse (seq (sanitize n)))) | |
) | |
(defn palindromes [text] | |
(let [result ()] | |
(for [i (range 0 (- (count text) 1))] | |
(let [character (get text i) | |
stop (.indexOf text (str character) i)] | |
(if (not (= -1 stop)) | |
(let [string (.substring text i stop)] | |
(if (palindromep string) (println string)) | |
) | |
) | |
) | |
) | |
) | |
) | |
(defn longest-palindrome [text] | |
(apply max (palindromes text)) | |
) | |
(palindromes text) | |
;;(println (longest-palindrome text)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment