Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,21 @@
|
|||
(def lowercase (map char (range (int \a) (inc (int \z)))))
|
||||
|
||||
(defn move-to-front [x xs]
|
||||
(cons x (remove #{x} xs)))
|
||||
|
||||
(defn encode [text table & {:keys [acc] :or {acc []}}]
|
||||
(let [c (first text)
|
||||
idx (.indexOf table c)]
|
||||
(if (empty? text) acc (recur (drop 1 text) (move-to-front c table) {:acc (conj acc idx)}))))
|
||||
|
||||
(defn decode [indices table & {:keys [acc] :or {acc []}}]
|
||||
(if (empty? indices) (apply str acc)
|
||||
(let [n (first indices)
|
||||
c (nth table n)]
|
||||
(recur (drop 1 indices) (move-to-front c table) {:acc (conj acc c)}))))
|
||||
|
||||
(doseq [word ["broood" "bananaaa" "hiphophiphop"]]
|
||||
(let [encoded (encode word lowercase)
|
||||
decoded (decode encoded lowercase)]
|
||||
(println (format "%s encodes to %s which decodes back to %s."
|
||||
word encoded decoded))))
|
||||
Loading…
Add table
Add a link
Reference in a new issue