diff options
| author | Tyler Wardhaugh <tyler.wardhaugh@gmail.com> | 2020-11-23 10:02:24 -0800 |
|---|---|---|
| committer | Tyler Wardhaugh <tyler.wardhaugh@gmail.com> | 2020-11-23 10:07:09 -0800 |
| commit | 54d4de3cf47f4350a04d8f5817e59513ad3b4660 (patch) | |
| tree | 6a9b6826d2e4c4d10bd4c121c06e1048cf452437 | |
| parent | e3b053b19638d19293f03917021c3e9720c248d0 (diff) | |
| download | perlweeklychallenge-club-54d4de3cf47f4350a04d8f5817e59513ad3b4660.tar.gz perlweeklychallenge-club-54d4de3cf47f4350a04d8f5817e59513ad3b4660.tar.bz2 perlweeklychallenge-club-54d4de3cf47f4350a04d8f5817e59513ad3b4660.zip | |
Ch88 (Clojure): Task 1
3 files changed, 34 insertions, 1 deletions
diff --git a/challenge-088/tyler-wardhaugh/clojure/deps.edn b/challenge-088/tyler-wardhaugh/clojure/deps.edn index 0bea934933..f09671a417 100644 --- a/challenge-088/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-088/tyler-wardhaugh/clojure/deps.edn @@ -1,6 +1,7 @@ {:paths ["src" "resources"] :deps {org.clojure/clojure {:mvn/version "1.10.1"} - net.mikera/core.matrix {:mvn/version "0.62.0"}} + net.mikera/core.matrix {:mvn/version "0.62.0"} + org.clojure/math.numeric-tower {:mvn/version "0.0.4"}} :aliases {:test {:extra-paths ["test"] :extra-deps {org.clojure/test.check {:mvn/version "1.0.0"}}} diff --git a/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/t1.clj b/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/t1.clj new file mode 100644 index 0000000000..354eef276d --- /dev/null +++ b/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/t1.clj @@ -0,0 +1,24 @@ +(ns tw.weekly.c88.t1 + (:require [clojure.edn :as edn]) + (:require [clojure.math.numeric-tower :as math]) + (:require [clojure.pprint :refer [cl-format]])) + +;;; +; Task description for TASK #1 › Array of Product +;;; + +(defn array-of-product + "Return an array @M where $M[i] is the product of all elements of @N except the index $N[i]." + [coll] + (let [freqs (frequencies coll) + product (transduce (map (partial apply math/expt)) * freqs) + cache-xf (map (juxt key (comp (partial / product) key))) + cache (into {} cache-xf freqs)] + (sequence (map cache) coll))) + +(defn -main + "Run Task 1 with a list of numbers N, defaulting to the + first example given in the task description." + [& args] + (let [N (or (some->> args (map edn/read-string)) [5 2 1 4 3])] + (cl-format true "@M = (~{~a~^, ~})" (array-of-product N)))) diff --git a/challenge-088/tyler-wardhaugh/clojure/test/tw/weekly/c88_test.clj b/challenge-088/tyler-wardhaugh/clojure/test/tw/weekly/c88_test.clj new file mode 100644 index 0000000000..0d3f2219ee --- /dev/null +++ b/challenge-088/tyler-wardhaugh/clojure/test/tw/weekly/c88_test.clj @@ -0,0 +1,8 @@ +(ns tw.weekly.c88-test + (:require [clojure.test :refer [deftest is testing]] + [tw.weekly.c88.t1 :refer [array-of-product]])) + +(deftest task-1 + (testing "Task 1 Array of Product" + (is (= [24 60 120 30 40] (array-of-product [5 2 1 4 3]))) + (is (= [12 24 6 8] (array-of-product [2 1 4 3]))))) |
