aboutsummaryrefslogtreecommitdiff
path: root/challenge-080
diff options
context:
space:
mode:
authorTyler Wardhaugh <tyler.wardhaugh@gmail.com>2020-09-28 15:25:25 -0700
committerTyler Wardhaugh <tyler.wardhaugh@gmail.com>2020-09-28 15:47:45 -0700
commit3db826ef9f6b2b73586d75f7b89c010a079c40a5 (patch)
tree031d623675c5281ae5ac5b2db61700d7cb70350e /challenge-080
parent73b8c8151d43eb4f6283c0bb93851d204cdee6ee (diff)
downloadperlweeklychallenge-club-3db826ef9f6b2b73586d75f7b89c010a079c40a5.tar.gz
perlweeklychallenge-club-3db826ef9f6b2b73586d75f7b89c010a079c40a5.tar.bz2
perlweeklychallenge-club-3db826ef9f6b2b73586d75f7b89c010a079c40a5.zip
Ch80: Task 2
Diffstat (limited to 'challenge-080')
-rw-r--r--challenge-080/tyler-wardhaugh/clojure/src/tw/weekly/c80/t2.clj27
-rw-r--r--challenge-080/tyler-wardhaugh/clojure/test/tw/weekly/c80_test.clj8
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))))