Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
4
Task/Permutations/Clojure/permutations-1.clj
Normal file
4
Task/Permutations/Clojure/permutations-1.clj
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
user=> (require 'clojure.contrib.combinatorics)
|
||||
nil
|
||||
user=> (clojure.contrib.combinatorics/permutations [1 2 3])
|
||||
((1 2 3) (1 3 2) (2 1 3) (2 3 1) (3 1 2) (3 2 1))
|
||||
35
Task/Permutations/Clojure/permutations-2.clj
Normal file
35
Task/Permutations/Clojure/permutations-2.clj
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
(defn- iter-perm [v]
|
||||
(let [len (count v),
|
||||
j (loop [i (- len 2)]
|
||||
(cond (= i -1) nil
|
||||
(< (v i) (v (inc i))) i
|
||||
:else (recur (dec i))))]
|
||||
(when j
|
||||
(let [vj (v j),
|
||||
l (loop [i (dec len)]
|
||||
(if (< vj (v i)) i (recur (dec i))))]
|
||||
(loop [v (assoc v j (v l) l vj), k (inc j), l (dec len)]
|
||||
(if (< k l)
|
||||
(recur (assoc v k (v l) l (v k)) (inc k) (dec l))
|
||||
v))))))
|
||||
|
||||
|
||||
(defn- vec-lex-permutations [v]
|
||||
(when v (cons v (lazy-seq (vec-lex-permutations (iter-perm v))))))
|
||||
|
||||
(defn lex-permutations
|
||||
"Fast lexicographic permutation generator for a sequence of numbers"
|
||||
[c]
|
||||
(lazy-seq
|
||||
(let [vec-sorted (vec (sort c))]
|
||||
(if (zero? (count vec-sorted))
|
||||
(list [])
|
||||
(vec-lex-permutations vec-sorted)))))
|
||||
|
||||
(defn permutations
|
||||
"All the permutations of items, lexicographic by index"
|
||||
[items]
|
||||
(let [v (vec items)]
|
||||
(map #(map v %) (lex-permutations (range (count v))))))
|
||||
|
||||
(println (permutations [1 2 3]))
|
||||
Loading…
Add table
Add a link
Reference in a new issue