Skip to content

Instantly share code, notes, and snippets.

View dirtyvagabond's full-sized avatar

Aaron Crow dirtyvagabond

  • Los Angeles, CA
View GitHub Profile
@dirtyvagabond
dirtyvagabond / best-stock-trade-fast.clj
Last active November 30, 2016 18:55
stock trader, O(n) time complexity
;; for context:
;; http://stackoverflow.com/questions/7086464/maximum-single-sell-profit/
(def PRICES [5 10 4 6 7 1 7])
(defn day [[ndx low profit sell-ndx] price]
(let [nprofit (max profit (- price (or low price)))
nlow (if low (min low price) price)
nsell-ndx (if (> nprofit profit) ndx sell-ndx)]
@dirtyvagabond
dirtyvagabond / best-stock-trade-2.clj
Last active November 21, 2016 02:00
naive stock trade picker, 20/20 hindsight, after help from amalloy
; element at index i represents the stock price at day i
(def PRICES [10 1 45 12 11 4 8 2])
; what is the best possible trade?
; (assumes 20/20 hindsight)
(defn possible-trades [prices]
(for [j (range (count prices))
i (range j)
:let [[buy sell] (map prices [i j])
@dirtyvagabond
dirtyvagabond / best-stock-trade.clj
Created November 19, 2016 00:54
naive stock trade picker, 20/20 hindsight
; element at index i represents the stock price at day i
(def PRICES [10 1 45 12 11 4 8 2])
; what is the best possible trade?
; (assumes 20/20 hindsight)
(defn possible-trades [prices]
(for [i (range (count prices))
j (range (count prices))
:when (< i j)
;;
;; Source code I used to demo mturk.clj
;;
;; added to templating.clj
(defn selection-q-form-xml [{:keys [instructions selections select-style]}]
(str XML-PREFIX
(html
[:QuestionForm {:xmlns "http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionForm.xsd"}
(defn vdiff [v1 v2]
(when (not= v1 v2)
[v1 v2]))
(defn when-diffs [m]
(when (some identity (vals m))
m))
(defn aaron-diff [x y]
(when-diffs
(defn run-opts [opts]
(let [opts (merge {:auto true} opts)]
(set-options opts)
(with-workflow-file #(run % (:targetv opts)))))
(defn -run_opts
"Explicitly for use from Java"
[opts]
(let [opts (into {} (for [[k v] opts] [(keyword k) v]))]
(run-opts opts)))
{ "address" : "82 Hawarden Crescent",
"address_extended" : "82 Hawarden Crescent, Sunderland, United Kingdom",
"category" : [ "establishment" ],
"country" : "GB",
"latitude" : 54.897776999999998,
"locality" : "Sunderland",
"longitude" : -1.406487,
"name" : "Wallseal Preservation",
"postcode" : "SR4 7NL",
"reference" : "CoQBcwAAAEwsjwzPGUmhVEFtVY9GL6QNwaLwGw1gLV0oPYPx53RufksQ1o1vdVtgopROoawFJX9JaIJfcLKsQqRA0PItkP5VWLoJ_3EpWlEu1EOfTkdV6Ai6lql0ehk-NbOqN1LT__S4RoMBKeITH_l4f05ODidOTWFAwUmaNYAdVMWZ1jESEhBFaq4z4-gUDB2-ssAOZ2alGhS0vrEQgDIquCWroCbh3Hc3hQStZQ",
{"address_components"
[{"long_name" "290", "short_name" "290", "types" ["street_number"]}
{"long_name" "Upper Richmond Rd",
"short_name" "Upper Richmond Rd",
"types" ["route"]}
{"long_name" "Putney",
"short_name" "Putney",
"types" ["locality" "political"]}
{"long_name" "London",
"short_name" "London",
;
; Fetches sorted people from HDFS, keeping only a select few, and
; sorting by name.
;
; Output is like:
; Artem Boytsov,310-100-0000
; Vinnie Pepi,+86-310-400-0000
; Will Lao,+86-310-600-0000
;
people <- hdfs://peeps/everyone
;
; Grabs us some data from the Internets
;
contracts.csv <-
curl http://www.ferc.gov/docs-filing/eqr/soft-tools/sample-csv/contract.txt > $OUTPUT
;
; Filters out all but the evergreen contracts
;
evergreens.csv <- contracts.csv