Created
July 6, 2012 19:24
-
-
Save uvtc/3062265 to your computer and use it in GitHub Desktop.
crunch-it (Clojure)
This file contains 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
(ns crunch-it.core | |
(:gen-class)) | |
(def sample-chars "abcdefghijkl0123456789") | |
(defn generate-random-strings | |
"Generates a list (`num` items long) of randomish 10-character strings." | |
[num] | |
(for [i (range num)] | |
(apply str (for [j (range 10)] | |
(rand-nth sample-chars))))) | |
(defn extract-biggish-nums | |
"Takes a list of 10-character strings that look like \"ab19da4890\", | |
and extracts numbers from them (3 digits (or more) in a row); in this | |
case, 4890. Returns the list of numbers extracted. Not all of the | |
10-digit strings will contain substantial sequences of digits." | |
[li] | |
(map (fn [x] (Long/parseLong x)) | |
(filter (fn [y] (not (nil? y))) | |
(for [st li] (re-find #"[1-9]\d{2,}" st))))) | |
(defn construct-distribution | |
"Pass in a list of ints. Returns a map of num-digits: num-ocurrences (that | |
is, how many 3-digit nums were in the list, how many 4-digit nums, 5-digit nums, | |
etc.)" | |
[li] | |
(frequencies (for [n li] (count (str n))))) | |
(defn print-distribution | |
[d] | |
(doseq [i (sort (keys d))] | |
(println " " i ": " (d i)))) | |
(defn -main | |
[& args] | |
(let [num (Integer/parseInt (str (first args))) | |
strs (generate-random-strings num) | |
nums (extract-biggish-nums strs) | |
mn (apply min nums) | |
mx (apply max nums) | |
d (construct-distribution nums) | |
logs (map (fn [x] (Math/log x)) | |
nums)] | |
(println "Generated" num "strings.") | |
(println "Found" (count nums) "numbers.") | |
(println "Min:" mn) | |
(println "Max:" mx) | |
(print-distribution d) | |
(println "Avg of logs of nums:" (/ (reduce + logs) | |
(count logs))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment