Skip to content

Instantly share code, notes, and snippets.

@kohyama kohyama/4clojure-s53.clj
Last active Oct 19, 2017

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

commented Jun 14, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.