diff options
| author | Tyler Wardhaugh <twardhaugh@cap-rx.com> | 2021-03-31 10:43:24 -0700 |
|---|---|---|
| committer | Tyler Wardhaugh <twardhaugh@cap-rx.com> | 2021-04-03 10:55:24 -0700 |
| commit | c8cf311317835bd08e0fdaf476519cf3de7ddf2c (patch) | |
| tree | a8c022a359db9ad27f1c97a6cd5469aa6a56cac6 | |
| parent | 132b4c1a86e21e2ad5e17364369c107dc17cb0fc (diff) | |
| download | perlweeklychallenge-club-c8cf311317835bd08e0fdaf476519cf3de7ddf2c.tar.gz perlweeklychallenge-club-c8cf311317835bd08e0fdaf476519cf3de7ddf2c.tar.bz2 perlweeklychallenge-club-c8cf311317835bd08e0fdaf476519cf3de7ddf2c.zip | |
Ch106 (Clojure): Tasks 1 & 2
7 files changed, 113 insertions, 6 deletions
diff --git a/challenge-106/tyler-wardhaugh/clojure/README.md b/challenge-106/tyler-wardhaugh/clojure/README.md index 469eabb6cb..4ad284eaa5 100644 --- a/challenge-106/tyler-wardhaugh/clojure/README.md +++ b/challenge-106/tyler-wardhaugh/clojure/README.md @@ -1,13 +1,13 @@ -# tw.weekly.c105 +# tw.weekly.c106 -The Weekly Challenge - #105 - Tyler Wardhaugh +The Weekly Challenge - #106 - Tyler Wardhaugh ## Usage Run the project directly (shows default output from both tasks): - $ clojure -M -m tw.weekly.c105.core + $ clojure -M -m tw.weekly.c106.core Run the project's tests (which are samples from the task descriptions): @@ -15,11 +15,11 @@ Run the project's tests (which are samples from the task descriptions): Run Task #1 with input - $ clojure -M -m tw.weekly.c105.t1 N K + $ clojure -M -m tw.weekly.c106.t1 N Run Task #2 with input: - $ clojure -M -m tw.weekly.c105.t2 NAME + $ clojure -M -m tw.weekly.c106.t2 N D ## Project Template diff --git a/challenge-106/tyler-wardhaugh/clojure/deps.edn b/challenge-106/tyler-wardhaugh/clojure/deps.edn index 32772ee93c..8b94f9a032 100644 --- a/challenge-106/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-106/tyler-wardhaugh/clojure/deps.edn @@ -1,6 +1,5 @@ {:paths ["src" "resources"] :deps {org.clojure/clojure {:mvn/version "1.10.1"} - org.apache.commons/commons-math3 {:mvn/version "3.6.1"} org.clojure/math.numeric-tower {:mvn/version "0.0.4"}} :aliases {:test {:extra-paths ["test"] diff --git a/challenge-106/tyler-wardhaugh/clojure/pom.xml b/challenge-106/tyler-wardhaugh/clojure/pom.xml index b6737dd838..ba1826c36c 100644 --- a/challenge-106/tyler-wardhaugh/clojure/pom.xml +++ b/challenge-106/tyler-wardhaugh/clojure/pom.xml @@ -30,6 +30,11 @@ <artifactId>clojure</artifactId> <version>1.10.1</version> </dependency> + <dependency> + <groupId>org.clojure</groupId> + <artifactId>math.numeric-tower</artifactId> + <version>0.0.4</version> + </dependency> </dependencies> <build> <sourceDirectory>src</sourceDirectory> diff --git a/challenge-106/tyler-wardhaugh/clojure/src/tw/weekly/c106/core.clj b/challenge-106/tyler-wardhaugh/clojure/src/tw/weekly/c106/core.clj new file mode 100644 index 0000000000..d6c8034916 --- /dev/null +++ b/challenge-106/tyler-wardhaugh/clojure/src/tw/weekly/c106/core.clj @@ -0,0 +1,12 @@ +(ns tw.weekly.c106.core + (:require [tw.weekly.c106.t1 :as t1]) + (:require [tw.weekly.c106.t2 :as t2]) + (:gen-class)) + +(defn -main + "Run all tasks" + [& _] + (println "Task #1:") + (t1/-main) + (println "\nTask #2:") + (t2/-main)) diff --git a/challenge-106/tyler-wardhaugh/clojure/src/tw/weekly/c106/t1.clj b/challenge-106/tyler-wardhaugh/clojure/src/tw/weekly/c106/t1.clj new file mode 100644 index 0000000000..fefdbafb79 --- /dev/null +++ b/challenge-106/tyler-wardhaugh/clojure/src/tw/weekly/c106/t1.clj @@ -0,0 +1,22 @@ +(ns tw.weekly.c106.t1 + (:require [clojure.edn :as edn])) + +;;; +; Task description for TASK #1 › Maximum Gap +;;; +(def DEFAULT-INPUT [2 9 3 5]) + +(defn maximum-gap + "Determine the maximum difference between two successive elements once coll + is sorted." + [coll] + (let [source (->> (sort coll) (partition 2 1)) + xf (map (fn [[x y]] (- y x)))] + (transduce xf max 0 source))) + +(defn -main + "Run Task 1 with a given N, defaulting to the example given in the task + description." + [& args] + (let [N (or (some-> args first edn/read-string) DEFAULT-INPUT)] + (println (maximum-gap N)))) diff --git a/challenge-106/tyler-wardhaugh/clojure/src/tw/weekly/c106/t2.clj b/challenge-106/tyler-wardhaugh/clojure/src/tw/weekly/c106/t2.clj new file mode 100644 index 0000000000..d48c6e3b0e --- /dev/null +++ b/challenge-106/tyler-wardhaugh/clojure/src/tw/weekly/c106/t2.clj @@ -0,0 +1,43 @@ +(ns tw.weekly.c106.t2 + (:require [clojure.edn :as edn] + [clojure.math.numeric-tower :refer [abs]])) + +;;; +; Task description for TASK #2 › Decimal String +;;; +(def DEFAULT-INPUT [1 3]) + +(defn- decimal-string-recurring + "Covert the given n[umerator] and d[enominator] (which must be known to + have a recurring fractional result) into a decimal string where the recurring + fractional is put in parenthesis." + [n d] + (loop [remainder (abs (rem n d)) + cache {} + result []] + (if (not (or (zero? remainder) (cache remainder))) + (recur (rem (* 10 remainder) d) + (assoc cache remainder (count (apply str result))) + (conj result (quot (* 10 remainder) d))) + (let [[lead recurrence] (->> result (split-at (get cache remainder 0))) + whole (quot n d) + pieces (concat [whole \.] lead [\(] recurrence [\)])] + (apply str pieces))))) + +(defn decimal-string + "Covert the given n[umerator] and d[enominator] into a decimal string where any + recurring fractional is put in parenthesis." + [n d] + (try (str (/ (bigdec n) d)) + (catch ArithmeticException _ + ; BigDecimal throws this exception when the result cannot be + ; represented with the 'UNLIMITED' precision (the default), i.e., + ; there is a recurring fractional result + (decimal-string-recurring n d)))) + +(defn -main + "Run Task 2 with a given N and D, defaulting to the example given in the task + description." + [& args] + (let [[N D] (or (some->> args (take 2) (map edn/read-string)) DEFAULT-INPUT)] + (println (decimal-string N D)))) diff --git a/challenge-106/tyler-wardhaugh/clojure/test/tw/weekly/c106_test.clj b/challenge-106/tyler-wardhaugh/clojure/test/tw/weekly/c106_test.clj new file mode 100644 index 0000000000..dad33dfd06 --- /dev/null +++ b/challenge-106/tyler-wardhaugh/clojure/test/tw/weekly/c106_test.clj @@ -0,0 +1,26 @@ +(ns tw.weekly.c106-test + (:require [clojure.test :refer [deftest is testing]] + [tw.weekly.c106.t1 :refer [maximum-gap]] + [tw.weekly.c106.t2 :refer [decimal-string]])) + +(deftest task-1 + (testing "Task 1, Maximum Gap" + (is (= 4 (maximum-gap [2, 9, 3, 5]))) + (is (= 5 (maximum-gap [1, 3, 8, 2, 0]))) + (is (= 0 (maximum-gap [5]))))) + +; additional test cases pulled from other submissions +(deftest task-2 + (testing "Task 2, Decimal String" + (is (= "0.(3)" (decimal-string 1 3))) + (is (= "0.5" (decimal-string 1 2))) + (is (= "0.0(75)" (decimal-string 5 66))) + (is (= "-3.(142857)" (decimal-string -22 7))) + (is (= "-0.375" (decimal-string 3 -8))) + (is (= "-11" (decimal-string -99 9))) + (is (= "0.(010309278350515463917525773195876288659793814432989690721649484536082474226804123711340206185567)" + (decimal-string 1 97))) + (is (= "5.(210084033613445378151260504201680672268907563025)" + (decimal-string 620 119))) + (is (= "0.0001(6)" (decimal-string 1 6000))) + (is (= "0.(0196078431372549)" (decimal-string 1 51))))) |
