From fa7cdeebec1933291947078ce2fef1aa3450a2b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20Norman=20Hauk=C3=A5s?= Date: Mon, 17 Nov 2014 12:45:40 +0100 Subject: [PATCH] found rainiest period. cleaned up tests. adjusted a bit of formatting. --- src/weather_data/rainiestday.clj | 1 - src/weather_data/rainiestperiod.clj | 52 ++++++++++------------- test/weather_data/rainiestperiod_test.clj | 20 +-------- 3 files changed, 24 insertions(+), 49 deletions(-) diff --git a/src/weather_data/rainiestday.clj b/src/weather_data/rainiestday.clj index 71e7a40..5032e99 100644 --- a/src/weather_data/rainiestday.clj +++ b/src/weather_data/rainiestday.clj @@ -1,4 +1,3 @@ - (ns weather-data.rainiestday (:require [clojure.data.csv :as csv] [clojure.java.io :as io])) diff --git a/src/weather_data/rainiestperiod.clj b/src/weather_data/rainiestperiod.clj index 4316dc9..c87cdd1 100644 --- a/src/weather_data/rainiestperiod.clj +++ b/src/weather_data/rainiestperiod.clj @@ -1,6 +1,3 @@ -; Finn mest regnfulle dag, -; lengste periode med regn - (ns weather-data.rainiestperiod (:require [clojure.data.csv :as csv] [clojure.java.io :as io] @@ -16,42 +13,37 @@ (try (Double. x) (catch Exception e 0))) -(defn rainLevel [entry] (convertToNumeric (nth entry 5))) - -(defn location [entry] (nth entry 0)) - -(defn rainy [entry] - (if (> (rainLevel entry) 0) true false)) - (defn parseTime [entry] (let [custom-formatter (f/formatter "dd.MM.YYYY")] (f/parse custom-formatter (nth entry 1)))) (defn consecutiveDay [entry1 entry2] - (or (= (t/plus (parseTime entry1) (t/days 1)) (parseTime entry2)) - (= (t/minus (parseTime entry1) (t/days 1)) (parseTime entry2)))) + (or (empty? entry1) + (= (t/plus (parseTime entry1) (t/days 1)) (parseTime entry2)) + (= (t/minus (parseTime entry1) (t/days 1)) (parseTime entry2)))) -(def listOfPlaces (partition-by (fn [entry] (nth entry 0)) (rest csv))) +(defn filterRain [place] + (letfn [(getRainLevel [entry] (convertToNumeric (nth entry 5)))] + (filter #(> (convertToNumeric (getRainLevel %)) 0) place))) -(def rolldal (nth listOfPlaces 0)) +(def rainObservationsByPlace + (letfn [(location [entry] (nth entry 0))] + (partition-by location (filterRain (rest csv))))) -(defn rainyEntries [place] (filter #(> (convertToNumeric (rainLevel %)) 0) place)) +(defn findRainyPeriods + "Builds nested list of rainy periods: [[[<-entry->]<-rainyperiods->]]" + ([remaining] (findRainyPeriods [[(first remaining)]] (rest remaining))) + ([result remaining] + (if(empty? remaining) result + (if(consecutiveDay (last (last result)) (first remaining)) + (recur (conj (vec (butlast result)) (conj (last result) (first remaining))) (rest remaining)) + (recur (conj result [(first remaining)]) (rest remaining)))))) -(defn addToLastPeriod [x y] - (vec (conj (butlast x) (vec (conj (last x) y))))) +(def rainyPeriods (map findRainyPeriods rainObservationsByPlace)) -(defn createNewPeriod [x y] - (vec (conj x [y]))) +(defn findRainiestPeriod [periods] + (let [rainiestPeriods (map #(last (sort-by count %)) periods)] + (last (sort-by count rainiestPeriods)))) -(defn rainyPeriods [raindata] (reduce - (fn [x y] - (if(= [[[]]] x) - (addToLastPeriod (last (last x)) y) - (if(consecutiveDay (last (last x)) y) - (do (println "addingToLastperiod") - (addToLastPeriod x y)) - (createNewPeriod x y)))) - [[[]]] - raindata)) +(findRainiestPeriod rainyPeriods) -(rainyPeriods (take 5 (rainyEntries rolldal))) diff --git a/test/weather_data/rainiestperiod_test.clj b/test/weather_data/rainiestperiod_test.clj index 274eea5..db973c3 100644 --- a/test/weather_data/rainiestperiod_test.clj +++ b/test/weather_data/rainiestperiod_test.clj @@ -1,28 +1,12 @@ (ns weather-data.rainiestperiod-test (:require [clojure.test :refer :all][weather-data.rainiestperiod :refer :all :as weather])) -(deftest add-1-to-1 - (is (= 2 (+ 1 1)))) - -(deftest testAddToLastPeriod - (let [x [[["entry1"]["entry2"]]] - y ["entry3"] - result [[["entry1"]["entry2"]["entry3"]]]] - (is (= (weather/addToLastPeriod x y) result)))) - -(deftest testCreateNewPeriod - (let [x [[["entry1"]["entry2"]]] - y ["entry3"] - result [[["entry1"]["entry2"]][["entry3"]]]] - (is (= (weather/createNewPeriod x y) result)))) - (deftest testConsecutiveDay (let [x [[["RØLDAL" "26.10.2014" "-" "-" "-" "42.7" "0" "0" "Regn"]]] y ["RØLDAL" "27.10.2014" "-" "-" "-" "57.0" "0" "0" "Regn"]] (is (weather/consecutiveDay (last (last x)) y)))) - -(def initData (take 5 (weather/rainyEntries weather/rolldal))) +(def initData (take 5 (first weather/rainObservationsByPlace))) (def result [[["RØLDAL" "01.01.1960" "-" "-" "-" "19.9" "25" "4" "Regn.snø.sludd"] ["RØLDAL" "02.01.1960" "-" "-" "-" "2.0" "19" "4" "Regn"]] [["RØLDAL" "05.01.1960" "-" "-" "-" "3.0" "15" "4" "Regn"] @@ -30,6 +14,6 @@ ["RØLDAL" "07.01.1960" "-" "-" "-" "0.2" "15" "4" "Regn"]]]) (deftest testRainyPeriods - (is (= (weather/rainyPeriods initData) result))) + (is (= (weather/findRainyPeriods initData) result))) (run-tests)