Created
July 29, 2015 21:06
-
-
Save maruks/80e47d753ee5986e0f25 to your computer and use it in GitHub Desktop.
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 dojo.core) | |
(def prices {:A 2.5 | |
:B 3.0}) | |
(def zones-map {"Asterix" :A | |
"Aldgate" :A | |
"Barbican" :B | |
"Balham" :B}) | |
(def daily-cap {:A 7.0 | |
:B 8.0}) | |
(defn journey [& args] | |
(let [zones (map zones-map (concat (map :from args) (map :to args))) | |
legs (partition 2 zones) | |
prices (map #(max ((first %) prices) ((second %) prices)) legs) | |
cap (reduce max (map daily-cap (distinct zones)))] | |
(min cap (reduce + prices)))) | |
(ns dojo.core-spec | |
(:require [speclj.core :refer :all] | |
[dojo.core :refer :all])) | |
(def journeys [{:from "Asterix" :to "Barbican"} | |
{:from "Barbican" :to "Asterix"} | |
{:from "Asterix" :to "Balham"} | |
{:from "Balham" :to "Asterix"}]) | |
(describe "Clam card" | |
(it "calculates the price for a single journey in zone A" | |
(should= 2.5 (journey {:from "Asterix" :to "Aldgate"}))) | |
(it "calculates the price for journeys over several zones" | |
(should= 6.0 (journey {:from "Asterix" :to "Barbican"} {:from "Barbican" :to "Balham"}))) | |
(it "has a daily cap" | |
(should= 8.0 (apply journey (flatten journeys)))) | |
(it "has a weekly cap" | |
(let [week-journeys (mapcat (fn [r] (map #(assoc % :day r) journeys)) (range 1 8))] | |
(should= 8.0 (apply journey week-journeys))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment