Last active
August 29, 2015 14:00
-
-
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
This file contains hidden or 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
(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)))}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's mine https://gist.github.com/clifton/11304681