Last active
December 24, 2015 03:09
-
-
Save olauzon/6735059 to your computer and use it in GitHub Desktop.
Ordered paths
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
(defn ordered-paths | |
[coll] | |
(let [len (count coll) | |
end (dec len)] | |
(cond | |
(zero? len) coll | |
(zero? end) coll | |
:else | |
(loop [start 0 | |
curr 1 | |
paths []] | |
(if (and (= start end) (> curr end)) paths | |
(do | |
(recur | |
(if (= curr end) (inc start) start) | |
(if (= curr end) (+ start 2) (inc curr)) | |
(conj paths [(nth coll start) (nth coll curr)])))))))) | |
(ordered-paths []) | |
;; [] | |
(ordered-paths [4]) | |
;; [4] | |
(ordered-paths [4 1 2 8 9]) | |
;; [[4 1] [4 2] [4 8] [4 9] [1 2] [1 8] [1 9] [2 8] [2 9] [8 9]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment