Last active
October 19, 2017 09:04
-
-
Save kohyama/2928234 to your computer and use it in GitHub Desktop.
A solution for #53 of 4clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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]) []))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://www.4clojure.com/problem/53