Data update

This commit is contained in:
Ingy döt Net 2026-02-01 16:33:20 -08:00
parent 5150844a7d
commit 4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions

View file

@ -14,7 +14,7 @@
(defn huffman-tree [pq]
(while (> (.size pq) 1)
(let [a (.poll pq) b (.poll pq)
new-node {:priority (+ (:priority a) (:priority b)) :left a :right b}]
new-node {:priority (+ (:priority a) (:priority b)) :left a :right b}]
(.add pq new-node)))
(.poll pq))

View file

@ -4,29 +4,29 @@
(defn init-pq [s]
(let [c (count s)]
(->> s frequencies
(map (fn [[k v]] [k {:sym k :weight (/ v c)}]))
(into (priority-map-keyfn-by :weight <)))))
(map (fn [[k v]] [k {:sym k :weight (/ v c)}]))
(into (priority-map-keyfn-by :weight <)))))
(defn huffman-tree [pq]
(letfn [(build-step
[pq]
(let [a (second (peek pq)) b (second (peek (pop pq)))
nn {:sym (str (:sym a) (:sym b))
:weight (+ (:weight a) (:weight b))
:left a :right b}]
(assoc (pop (pop pq)) (:sym nn) nn)))]
[pq]
(let [a (second (peek pq)) b (second (peek (pop pq)))
nn {:sym (str (:sym a) (:sym b))
:weight (+ (:weight a) (:weight b))
:left a :right b}]
(assoc (pop (pop pq)) (:sym nn) nn)))]
(->> (iterate build-step pq)
(drop-while #(> (count %) 1))
first vals first)))
(drop-while #(> (count %) 1))
first vals first)))
(defn symbol-map [m]
(letfn [(sym-step
[{:keys [sym weight left right] :as m} code]
(cond (and left right) #(vector (trampoline sym-step left (str code \0))
(trampoline sym-step right (str code \1)))
left #(sym-step left (str code \0))
right #(sym-step right (str code \1))
:else {:sym sym :weight weight :code code}))]
[{:keys [sym weight left right] :as m} code]
(cond (and left right) #(vector (trampoline sym-step left (str code \0))
(trampoline sym-step right (str code \1)))
left #(sym-step left (str code \0))
right #(sym-step right (str code \1))
:else {:sym sym :weight weight :code code}))]
(trampoline sym-step m "")))
(defn huffman-encode [s]