Skip to content

Instantly share code, notes, and snippets.

View dkmarley's full-sized avatar

David Marley dkmarley

View GitHub Profile
(defn profits
"Takes a sequence of prices representing prices on consecutive days and returns a sequence of tuples containing
possible profitable transactions resulting from buying and selling on those days. The first member of the tuple
is a vector containing the 0 based day of purchase and sale. The second member is the profit from the transaction.
E.g. an input of [1 7 1 5 4 20] will return
([[0 1] 6] [[0 3] 4] [[0 4] 3] [[0 5] 19] [[1 5] 13] [[2 3] 4] [[2 4] 3] [[2 5] 19] [[3 5] 15] [[4 5] 16])"
[prices]
(apply
concat
(keep-indexed