Last active
August 29, 2015 14:00
-
-
Save clifton/11304681 to your computer and use it in GitHub Desktop.
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
(defn minmax-by-codepoint | |
"Takes a string and outputs the most and least common | |
characters along with their total counts. In the event of a | |
tie, return the character with the lowest code point" | |
[s] | |
(->> (frequencies s) | |
((juxt (partial sort-by (fn [[c cnt]] [cnt c])) | |
(partial sort-by (fn [[c cnt]] [(- cnt) c])))) | |
(map first))) | |
(comment | |
(= [[\d 1] [\i 7]] | |
(minmax-by-codepoint "supercalifragilisticexpialidocious")) | |
(= [[\space 1] [\c 3]] | |
(minmax-by-codepoint "technicolor dreamcoat"))) |
Beautiful!
Beautiful indeed. This is what a solution to this kind of problem should look like.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
not english.