daily programming simple list stuff

Jan 11, 2016  

Since I’m warming up to Clojure this year, why not get started with the “dailyprogrammer” subreddit again?

So here’s a (noobish) solution to this problem:

(ns dailyprog.core
  (:require [clojure.string :as str]))

(defn input->float
  (map #(Float/parseFloat %) (str/split input #" ")))

(defn difference
  "Given a list of prices, a price in the list and its index, return the best price to sell it at"
  [lst index buy-price]
  (let [max-sell-price (apply max (drop index lst))]
    {:buy-price buy-price
    :sell-price max-sell-price
    :difference (- max-sell-price buy-price)}))

(defn difference-vector
  (map-indexed (partial difference lst) lst))

(defn -main
  [& args]
  (println(->> (slurp (first args))
                (apply max-key :difference))))

and it runs like this:

> lein run /tmp/challenge-input
{:buy-price 8.03, :sell-price 10.02, :difference 1.9900007247924805}