Skip to content

Instantly share code, notes, and snippets.

@amalloy
Forked from kpmaynard/gist:8367053
Last active January 2, 2016 21:59
Show Gist options
  • Select an option

  • Save amalloy/8367369 to your computer and use it in GitHub Desktop.

Select an option

Save amalloy/8367369 to your computer and use it in GitHub Desktop.

Revisions

  1. amalloy revised this gist Jan 11, 2014. 2 changed files with 9 additions and 21 deletions.
    9 changes: 9 additions & 0 deletions gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    (defn permx [coll]
    (letfn [(helper [m]
    (if (empty? m)
    '(())
    (for [[i x] m
    perm (helper (dissoc m i))]
    (cons x perm))))]
    (helper (into (sorted-map)
    (map-indexed vector coll)))))
    21 changes: 0 additions & 21 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,21 +0,0 @@
    (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)))))
  2. @kpmaynard kpmaynard created this gist Jan 11, 2014.
    21 changes: 21 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    (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)))))