Skip to content

Instantly share code, notes, and snippets.

@daviddavis
Created March 22, 2011 01:45
Show Gist options
  • Save daviddavis/880608 to your computer and use it in GitHub Desktop.
Save daviddavis/880608 to your computer and use it in GitHub Desktop.
(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