diff options
| -rw-r--r-- | challenge-080/tyler-wardhaugh/clojure/src/tw/weekly/c80/t2.clj | 27 | ||||
| -rw-r--r-- | challenge-080/tyler-wardhaugh/clojure/test/tw/weekly/c80_test.clj | 8 |
2 files changed, 34 insertions, 1 deletions
diff --git a/challenge-080/tyler-wardhaugh/clojure/src/tw/weekly/c80/t2.clj b/challenge-080/tyler-wardhaugh/clojure/src/tw/weekly/c80/t2.clj new file mode 100644 index 0000000000..8d12ce4e14 --- /dev/null +++ b/challenge-080/tyler-wardhaugh/clojure/src/tw/weekly/c80/t2.clj @@ -0,0 +1,27 @@ +(ns tw.weekly.c80.t2 + (:require [clojure.edn :as edn])) + +;;; Task description for TASK #2 › Count Candies +; Submitted by: Mohammad S Anwar +; You are given rankings of @N candidates. +; +; Write a script to find out the total candies needed for all candidates. You are asked to follow the rules below: +; +; a) You must given at least one candy to each candidate. +; b) Candidate with higher ranking get more candies than their mmediate neighbors on either side. +;;; + +(defn count-candies + "Determine the number of candies needed according to the rules in the task description." + [coll] + (let [xf (comp (map (juxt (fn [[a b _]] (if (> b a) 1 0)) + (fn [[_ b c]] (if (> b c) 1 0)))) + (map (partial apply + 1))) + source (partition 3 1 (repeat ##Inf) (concat [##Inf] coll))] + (transduce xf + source))) + +(defn -main + "Run Task 2 with a list of integers N, defaulting to the first one given in the examples." + [& args] + (let [N (or (some->> args (map edn/read-string)) [1 2 2])] + (println (count-candies N)))) diff --git a/challenge-080/tyler-wardhaugh/clojure/test/tw/weekly/c80_test.clj b/challenge-080/tyler-wardhaugh/clojure/test/tw/weekly/c80_test.clj index 23d6d1772c..f940c2dc50 100644 --- a/challenge-080/tyler-wardhaugh/clojure/test/tw/weekly/c80_test.clj +++ b/challenge-080/tyler-wardhaugh/clojure/test/tw/weekly/c80_test.clj @@ -1,6 +1,7 @@ (ns tw.weekly.c80-test (:require [clojure.test :refer [deftest is testing]] - [tw.weekly.c80.t1 :refer [smallest-missing]])) + [tw.weekly.c80.t1 :refer [smallest-missing]] + [tw.weekly.c80.t2 :refer [count-candies]])) (deftest task-1 (testing "Task 1, Smallest Positive Number Bits" @@ -8,3 +9,8 @@ (is (= (smallest-missing [1 8 -1]) 2)) (is (= (smallest-missing [2 0 -1]) 1)) (is (nil? (smallest-missing [1 2 3]))))) + +(deftest task-2 + (testing "Task 2, Count Candies" + (is (= (count-candies [1, 2, 2]) 4)) + (is (= (count-candies [1, 4, 3, 2]) 7)))) |
