Last active
October 6, 2016 02:47
-
-
Save HerbM/c1277cf2df3a4c25dee84e0fb824e3ba to your computer and use it in GitHub Desktop.
This file contains hidden or 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
;; My solution | |
(defn intervs [s] | |
(let [sorted (sort (into #{} s)) | |
fs (first sorted)] | |
(cond (not (seq sorted)) () | |
(= 1 (count sorted)) (list [fs fs]) | |
(not= (inc fs) (second sorted)) (cons [fs fs] (intervs (rest sorted))) | |
:ELSE | |
(let [pairs (partition 2 1 sorted)] | |
(let [contig (take-while #(= (inc (first %)) (second %)) pairs) | |
more (drop-while #(= (inc (first %)) (second %)) pairs)] | |
(cons [(first (first contig)) (last (last contig))] | |
(intervs (rest (flatten more))))))))) | |
;;0trey's solution: | |
#(map vector (% %2 dec) (% %2 inc)) | |
#(reduce (fn [a b] (if ((set %) (%2 b)) a (conj a b))) [] (sort (set %))) | |
(= (#(map vector (% %2 dec) (% %2 inc)) | |
#(reduce (fn [a b] (if ((set %) (%2 b)) a (conj a b))) [] (sort (set %))) | |
[1 2 3]) [[1 3]]) | |
(= (#(map vector (% %2 dec) (% %2 inc)) | |
#(reduce (fn [a b] (if ((set %) (%2 b)) a (conj a b))) [] (sort (set %))) | |
[10 9 8 1 2 3]) [[1 3] [8 10]]) | |
(= (#(map vector (% %2 dec) (% %2 inc)) | |
#(reduce (fn [a b] (if ((set %) (%2 b)) a (conj a b))) [] (sort (set %))) | |
[1 1 1 1 1 1 1]) [[1 1]]) | |
(= (#(map vector (% %2 dec) (% %2 inc)) | |
#(reduce (fn [a b] (if ((set %) (%2 b)) a (conj a b))) [] (sort (set %))) | |
[]) []) | |
(= (#(map vector (% %2 dec) (% %2 inc)) | |
#(reduce (fn [a b] (if ((set %) (%2 b)) a (conj a b))) [] (sort (set %))) | |
[19 4 17 1 3 10 2 13 13 2 16 4 2 15 13 9 6 14 2 11]) | |
[[1 4] [6 6] [9 11] [13 17] [19 19]]) | |
;; ;; austintaylor's solution:-- I think I should have been able to see this one (now that I see it) | |
#(reduce | |
(fn [m a] | |
(if (or (empty? m) (not= (inc (last (last m))) a)) | |
(concat m [[a a]]) | |
(concat (butlast m) [[(first (last m)) a]]))) | |
[] (sort (set %))) | |
;;quant1's solution: | |
(fn [x] | |
(->> (sort x) distinct (interleave (range)) (partition 2) | |
(partition-by #(apply - %)) (map #(map second %)) | |
(map (juxt first last)))) | |
;; ctzsm's solution: I can work through this but have no idea how ctzsm thought it up | |
(fn [s] | |
(map #(vector (second (first %)) (second (last %))) | |
(partition-by #(first %) (map-indexed #(vector (- %2 %1) %2) (set s))))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment