Skip to content

Instantly share code, notes, and snippets.

@clifton
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clifton/11304681 to your computer and use it in GitHub Desktop.
Save clifton/11304681 to your computer and use it in GitHub Desktop.
(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")))
@tylerlee
Copy link

not english.

@hadronzoo
Copy link

Beautiful!

@camdez
Copy link

camdez commented Apr 25, 2014

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