I actually played with this on Saturday morning while waiting for a friend, and
thought it was an interesting problem.
Your example is spending most of its time filtering with palindrome? across all
682695 items generated by all-combs. To see, try:
(defn tony []
(def source "I like racecars that go fast!")
(def source2
"FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth")
(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)]
(do
(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 source
ac1 (all-combs input)
ac2 (all-combs source2)
palindromes-1 (filter palindrome? ac1)
palindromes-2 (filter palindrome? ac2)]
(println "all-combs(input1):")
(time (println (count (all-combs input))))
(println "all-combs(input2):")
(time (println (count (all-combs source2))))
(println "Filter(input1):")
(time (println (count (filter palindrome? ac1))))
(println "Filter(input2):")
(time (println (count (filter palindrome? ac2))))
(println palindromes-1)
(println palindromes-2)))
(tony)
> It looks a bit procedural.
Looks pretty functional to me :)
Just as a comparison, my solution takes this long, and I highly doubt its
efficiency:
$ time ./launch.sh 1.clj
***output removed***
real 1m19.176s
user 1m13.830s
sys 0m1.636s
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en