Skip to content

Instantly share code, notes, and snippets.

@stathissideris
Last active September 25, 2015 11:48
Show Gist options
  • Save stathissideris/916746 to your computer and use it in GitHub Desktop.
Save stathissideris/916746 to your computer and use it in GitHub Desktop.
optimisation
(defn lcs [xr yr]
((fn inner-lcs [x y]
(cond
(or (zero? (count x)) (zero? (count y))) nil
(= (first x) (first y)) (cons (first x) (inner-lcs (rest x) (rest y)))
:else (let [xx (inner-lcs (rest x) y)
yy (inner-lcs x (rest y))]
(if (> (count xx) (count yy)) xx yy)))) xr yr))
> (print "result:" (lcs "Stathis Georgiou Sideris" "Stathis Sideris") "\n")
result: (S t a t h i s S i d e r i s)
> (print "list result:" (prn-str (lcs [1 2 3 4 5 6 7] [-1 -2 3 4 7 8 9])))
list result: (3 4 7)
@stathissideris
Copy link
Author

Executes in 13% of the previous time

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