Friday, February 19, 2016

Tiny job application problem solved in Clojure

Today Álvaro García passed me this problem to play with.

I solved it first in the REPL:

Then I collected all the code in the extract-info function:

4 comments:

  1. That's a neat little test. I had a go before comparing yours with mine:

    (as-> text $
    (frequencies $)
    (sort-by val $)
    (reverse $)
    (map first $)
    (apply str $)
    (clojure.string/replace $ #"_.+" ""))

    Six of one, half dozen the other I guess. ;-)

    ReplyDelete
  2. My variation:

    (def input "epqiiqwdiwgyka_vsqtsu.....
    (->> input
    frequencies
    (sort-by val)
    (map key)
    reverse
    (apply str)
    (re-find #"[^_]+"))

    And with Bash:

    grep -o . input|sort|uniq -c|sort -nr|awk '{printf $2}'|grep -oP "[^_]+"|head -1

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. The reverse operation can be done away with:

    (def input "epqiiqwdiwgyka_vsqtsu.....
    (->> input
    frequencies
    (sort-by #(-' (val %)))
    (map key)
    (apply str)
    (re-find #"[^_]+"))

    ReplyDelete