diff options
4 files changed, 78 insertions, 0 deletions
diff --git a/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/core.clj b/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/core.clj new file mode 100644 index 0000000000..296c2c6051 --- /dev/null +++ b/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/core.clj @@ -0,0 +1,12 @@ +(ns tw.weekly.c90.core + (:require [tw.weekly.c90.t1 :as t1]) + (:require [tw.weekly.c90.t2 :as t2]) + (:gen-class)) + +(defn -main + "Run all tasks" + [& _] + (println "Task #1:") + (t1/-main) + (println "\nTask #2:") + (t2/-main)) diff --git a/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/t1.clj b/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/t1.clj new file mode 100644 index 0000000000..e739241345 --- /dev/null +++ b/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/t1.clj @@ -0,0 +1,28 @@ +(ns tw.weekly.c90.t1 + (:require [clojure.string :as str]) + (:require [clojure.pprint :refer [cl-format]]) + (:require [clojure.edn :as edn])) + +;;; +; Task description for TASK #1 › DNA Sequence +;;; + +(def DEFAULT-DNA + "GTAAACCCCTTTTCATTTAGACAGATCGACTCCTTATCCATTCTCAGAGATGTGTTGCTGGTCGCCG") + +(defn process-dna + "Process the DNA sequence, counting nucleiobases and determining the + complementary sequence." + [dna] + (let [complement-dna (fn [s] (str/escape s {\T \A, \A \T, \C \G, \G \C}))] + ((juxt frequencies complement-dna) dna))) + +(defn -main + "Run Task 1 with a given DNA sequence, defaulting to the example given in + the task description." + [& args] + (let [dna (or (some-> args first edn/read-string) DEFAULT-DNA) + [freqs complement-seq] (process-dna dna)] + (cl-format true "~12a: ~a~%~12a: ~a~%" + "Counts" freqs + "Complement" complement-seq))) diff --git a/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/t2.clj b/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/t2.clj new file mode 100644 index 0000000000..ab767f91fc --- /dev/null +++ b/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/t2.clj @@ -0,0 +1,22 @@ +(ns tw.weekly.c90.t2 + (:require [clojure.edn :as edn])) + +;;; +; Task description for TASK #2 › Ethiopian Multiplication +;;; + +(defn ethiopian-multiply + "Compute the product of two positive integers using Ethiopian Multiplication." + [a b] + (let [source (map vector (iterate #(quot % 2) a) (iterate #(* 2 %) b)) + xf (comp + (take-while (fn [[a _]] (pos? a))) + (keep (fn [[a b]] (when (odd? a) b))))] + (transduce xf + source))) + +(defn -main + "Run Task 2 with the given positive integers, defaulting to the example + given in the explanation page linked from the task description." + [& args] + (let [[A B] (or (some->> args (take 2) (map edn/read-string)) [12 14])] + (println (ethiopian-multiply A B)))) diff --git a/challenge-090/tyler-wardhaugh/clojure/test/tw/weekly/c90_test.clj b/challenge-090/tyler-wardhaugh/clojure/test/tw/weekly/c90_test.clj new file mode 100644 index 0000000000..a09788f137 --- /dev/null +++ b/challenge-090/tyler-wardhaugh/clojure/test/tw/weekly/c90_test.clj @@ -0,0 +1,16 @@ +(ns tw.weekly.c90-test + (:require [clojure.test :refer [deftest is testing]] + [tw.weekly.c90.t1 :refer [DEFAULT-DNA process-dna]] + [tw.weekly.c90.t2 :refer [ethiopian-multiply]])) + +(def COMPLEMENT-DEFAULT-DNA + "CATTTGGGGAAAAGTAAATCTGTCTAGCTGAGGAATAGGTAAGAGTCTCTACACAACGACCAGCGGC") + +(deftest task-1 + (testing "Task 1, DNA Sequence" + (is (= [{\G 13, \T 22, \A 14, \C 18}, COMPLEMENT-DEFAULT-DNA] + (process-dna DEFAULT-DNA))))) + +(deftest task-2 + (testing "Task 2, Ethiopian Multiplication" + (is (= 168 (ethiopian-multiply 12 14))))) |
