Skip to content

Instantly share code, notes, and snippets.

@hadronzoo
Last active August 29, 2015 14:00
Show Gist options
  • Save hadronzoo/11302433 to your computer and use it in GitHub Desktop.
Save hadronzoo/11302433 to your computer and use it in GitHub Desktop.
Select low and high character frequencies, choosing the lowest code point in case of a tie
(def input "bbbddddaaacccc")
(defn- lowest-code-point [coll]
(first (into (sorted-map-by #(< (int %1) (int %2))) coll)))
(defn- take-while= [value coll]
(take-while (fn [[_ v]] (== value v)) coll))
(let [counts (frequencies input)
sorted (sorted-map-by
(fn [k1 k2]
(let [v1 (counts k1)
v2 (counts k2)]
(if (== v1 v2)
(.compareTo k2 k1)
(.compareTo v2 v1)))))
sorted-counts (vec (into sorted counts))
[_ high-count] (first sorted-counts)
[_ low-count] (last sorted-counts)]
{:high (lowest-code-point (take-while= high-count sorted-counts))
:low (lowest-code-point (take-while= low-count (reverse sorted-counts)))})
@clifton
Copy link

clifton commented Apr 25, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment