The 'palindrome?' function can be made much faster. Your version -- which is idiomatic and fine when perf isn't a factor -- turns the test string into a sequence, reverses it, turns it back into a string, then checks for full equality with the original. There are faster (if uglier) ways to check for palindromes. This version finds the Level 1 answer in about 150ms on my machine:
(defn palindrome? [^String s] (let [len (.length s) mid (quot len 2)] (loop [i 0 j (dec len)] (if (= i mid) true (when (= (.charAt s i) (.charAt s j)) (recur (inc i) (dec j))))))) Also, the built-in function max-key will do what your sort/max-comp code is doing more concisely: (apply max-key count (filter palindrome? (all-combs input))) HTH, Justin On Oct 12, 3:02 pm, tonyl <celtich...@gmail.com> wrote: > Hi, I just started to learn clojure in a more serious way and I am > doing the first level of the greplin challenge. > > I made it to work with a short palindrome like the example they give > me, but when it comes to work with the input file, it takes for ever > and I have to stop it. > > $ time clj level1.clj > ^C > real 11m35.477s > user 1m44.431s > sys 9m3.878s > > This is my code: > > (defn palindrome? [s] > (= s (reduce str (reverse s)))) > > (defn all-combs [in] > (let [len (count in)] > (for [i (range 0 (- len 2)), j (range (+ i 1) len)] > (subs in i j)))) > > (defn max-comp [x y] > (let [lenx (count x), leny (count y)] > (cond (< lenx leny) 1 > (= lenx leny) 0 > (> lenx leny) -1))) > > ;;(let [input "I like racecars that go fast"] > (let [input (slurp "../_input/level1.in")] > (println (nth (sort max-comp (filter palindrome? (all-combs > input))) 0))) > > The input file is thishttp://challenge.greplin.com/static/gettysburg.txt > > It looks a bit procedural. It is long, but I don't think is the > biggest bottleneck, I think it is my approach to create all the > combinations possible for substrings. Maybe I should be using a lazy > seq? How would I go to do that? -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en