Skip to content

Instantly share code, notes, and snippets.

@kpmaynard
Created January 11, 2014 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kpmaynard/8367053 to your computer and use it in GitHub Desktop.
Save kpmaynard/8367053 to your computer and use it in GitHub Desktop.
Permutations. Specification for permutations. Recursive formula. Let P represent Permutations of a sequence (a1, a2, a3, ... an) P()=() P(a1) = (a1) P(a1 a2) = ((a2 a1) (a1 a2)) <= (map #(weave a2 %) P(a1)) P(a1 a2 a3) = ((a3 a2 a1) (a2 a3 a1) (a2 a1 a3) (a3 a1 a2) (a1 a3 a2) ( a1 a2 a3))... => (map #(weave a3 %) P(a1 a2)) Assume P(a1, a2,.... a…
(defn weave
([a-seq]
(weave (first a-seq) (rest a-seq)))
([x a-seq]
(defn weave-helper [x a-seq acc n]
(println "In weave: Sequence is: " a-seq)
(cond (nil? a-seq) (cons x '())
(<= n (count a-seq))
(weave-helper x a-seq (cons (concat (take n a-seq) (cons x (drop n a-seq))) acc) (inc n))
:else acc
))
(weave-helper x a-seq '() 0)))
(defn permx [a-seq]
(if (nil? a-seq)
nil
(map #(weave (first a-seq) %) (permx (rest a-seq)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment