Skip to content

Instantly share code, notes, and snippets.

@kohyama
Last active October 19, 2017 09:04
Show Gist options
  • Save kohyama/2928234 to your computer and use it in GitHub Desktop.
Save kohyama/2928234 to your computer and use it in GitHub Desktop.
A solution for #53 of 4clojure
(require '[clojure.test :refer :all])
(def s53
; If you are in 4clojure paste from here
(fn [coll]
(->> coll
; 1st. Make tails of coll.
(#(take-while identity (iterate next %)))
; 2nd. Take only consecutive elements from the head for each list.
(map (fn [[c & cs]]
(loop [[x & xs] cs acc [c] z c]
(if (= (inc z) x) (recur xs (conj acc x) x) acc))))
; 3rd. Take only vectors having 2 or more elements.
(filter #(< 1 (count %)))
; 4th. Take the longest. Take the one appearing first if 2 or more have a same length.
(reduce #(if (< (count %2) (count %)) % %2) [])
))
; to here.
)
(deftest s53-test
(is (= (s53 [1 0 1 2 3 0 4 5]) [0 1 2 3]))
(is (= (s53 [5 6 1 3 2 7]) [5 6]))
(is (= (s53 [2 3 3 4 5]) [3 4 5]))
(is (= (s53 [7 6 5 4]) [])))
@kohyama
Copy link
Author

kohyama commented Jun 14, 2012

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