aboutsummaryrefslogtreecommitdiff
path: root/challenge-106/tyler-wardhaugh/clojure/src/tw
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-106/tyler-wardhaugh/clojure/src/tw')
-rw-r--r--challenge-106/tyler-wardhaugh/clojure/src/tw/weekly/c106/core.clj12
-rw-r--r--challenge-106/tyler-wardhaugh/clojure/src/tw/weekly/c106/t1.clj22
-rw-r--r--challenge-106/tyler-wardhaugh/clojure/src/tw/weekly/c106/t2.clj43
3 files changed, 77 insertions, 0 deletions
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))))