Skip to content

Instantly share code, notes, and snippets.

@RobertARandolph
Created October 4, 2012 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobertARandolph/3836958 to your computer and use it in GitHub Desktop.
Save RobertARandolph/3836958 to your computer and use it in GitHub Desktop.
(defn longest-palindrome [s]
(let [palindrome? #(= (seq %) (reverse %))
slice-all (fn [s] (mapcat #(partition % 1 s) (mapcat list (range (count s) 1 -1))))]
(some palindrome? (slice-all s))))
;; or to return palindrome
(defn longest-palindrome [s]
(let [palindrome? #(= (seq %) (reverse %))
slice-all (fn [s] (mapcat #(partition % 1 s) (mapcat list (range (count s) 1 -1))))]
(first (filter palindrome? (slice-all s)))))
@RobertARandolph
Copy link
Author

To get actual palindrome:

(defn longest-palindrome [s](let [palindrome? #%28= %28seq %%29 %28reverse %%29%29
slice-all %28fn [s] %28mapcat #%28partition % 1 s%29 %28mapcat list %28range %28count s%29 1 -1%29%29%29%29]
%28first %28filter palindrome? %28slice-all s%29%29%29))

@RobertARandolph
Copy link
Author

Ignore that last comment :|

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment