Skip to content

Instantly share code, notes, and snippets.

@uvtc
Created July 6, 2012 19:24
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 uvtc/3062265 to your computer and use it in GitHub Desktop.
Save uvtc/3062265 to your computer and use it in GitHub Desktop.
crunch-it (Clojure)
(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)))))
@amalloy
Copy link

amalloy commented Jul 6, 2012

(defn extract-biggish-nums [li]
  (for [s li,
        match (re-seq #"[1-9]\d{2,}" s)]
    (Long/parseLong match)))

@uvtc
Copy link
Author

uvtc commented Jul 6, 2012

Nice. Thanks, amalloy. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment