Created
January 13, 2015 02:18
-
-
Save djtrack16/bf0e3e6507b016696f78 to your computer and use it in GitHub Desktop.
given a string and rank, find the permutation is at that index of the sorted permutations
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
(fn [s rank] | |
(let [n (count s) step (reduce * (range 1 n))] | |
(loop [i 0 s (sort s) acc '() r rank step step] | |
(let [index (quot r step)] | |
(if (= 1 (count s)) | |
(clojure.string/join (concat acc s)) | |
(recur (inc i) | |
(concat (take index s) (drop (inc index) s)) | |
(concat acc [(nth s index)]) | |
(- r (* index step)) | |
(quot step (- n (inc i))))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment