From cfb51189b52bda76f0536d7674e7247b15b9f09b Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Tue, 19 Jan 2021 14:53:07 -0800 Subject: Ch96 (Clojure): Task 1 & Task 2 --- challenge-096/tyler-wardhaugh/clojure/README.md | 10 +++---- challenge-096/tyler-wardhaugh/clojure/deps.edn | 9 +++---- challenge-096/tyler-wardhaugh/clojure/pom.xml | 14 +++++----- .../clojure/src/tw/weekly/c96/core.clj | 12 +++++++++ .../clojure/src/tw/weekly/c96/t1.clj | 25 +++++++++++++++++ .../clojure/src/tw/weekly/c96/t2.clj | 31 ++++++++++++++++++++++ .../clojure/test/tw/weekly/c96_test.clj | 16 +++++++++++ 7 files changed, 100 insertions(+), 17 deletions(-) create mode 100644 challenge-096/tyler-wardhaugh/clojure/src/tw/weekly/c96/core.clj create mode 100644 challenge-096/tyler-wardhaugh/clojure/src/tw/weekly/c96/t1.clj create mode 100644 challenge-096/tyler-wardhaugh/clojure/src/tw/weekly/c96/t2.clj create mode 100644 challenge-096/tyler-wardhaugh/clojure/test/tw/weekly/c96_test.clj diff --git a/challenge-096/tyler-wardhaugh/clojure/README.md b/challenge-096/tyler-wardhaugh/clojure/README.md index c83f0cfefa..0e314a09c0 100644 --- a/challenge-096/tyler-wardhaugh/clojure/README.md +++ b/challenge-096/tyler-wardhaugh/clojure/README.md @@ -1,13 +1,13 @@ -# tw.weekly.c92 +# tw.weekly.c96 -The Weekly Challenge - #092 - Tyler Wardhaugh +The Weekly Challenge - #096 - Tyler Wardhaugh ## Usage Run the project directly (shows default output from both tasks): - $ clojure -M -m tw.weekly.c92.core + $ clojure -M -m tw.weekly.c96.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.c92.t1 A B + $ clojure -M -m tw.weekly.c96.t1 S Run Task #2: - $ clojure -M -m tw.weekly.c92.t2 N S + $ clojure -M -m tw.weekly.c96.t2 S1 S2 ## Project Template diff --git a/challenge-096/tyler-wardhaugh/clojure/deps.edn b/challenge-096/tyler-wardhaugh/clojure/deps.edn index 12e0c0d36b..3702e967c4 100644 --- a/challenge-096/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-096/tyler-wardhaugh/clojure/deps.edn @@ -1,6 +1,5 @@ {:paths ["src" "resources"] - :deps {org.clojure/clojure {:mvn/version "1.10.1"} - org.clojure/data.finger-tree {:mvn/version "0.0.3"}} + :deps {org.clojure/clojure {:mvn/version "1.10.1"}} :aliases {:test {:extra-paths ["test"] :extra-deps {org.clojure/test.check {:mvn/version "1.0.0"}}} @@ -10,6 +9,6 @@ :sha "f7ef16dc3b8332b0d77bc0274578ad5270fbfedd"}} :main-opts ["-m" "cognitect.test-runner" "-d" "test"]} - :uberjar {:extra-deps {seancorfield/depstar {:mvn/version "1.0.95"}} - :main-opts ["-m" "hf.depstar.uberjar" "tw.weekly.c95.jar" - "-C" "-m" "tw.weekly.c95"]}}} + :uberjar {:extra-deps {seancorfield/depstar {:mvn/version "1.0.96"}} + :main-opts ["-m" "hf.depstar.uberjar" "tw.weekly.c96.jar" + "-C" "-m" "tw.weekly.c96"]}}} diff --git a/challenge-096/tyler-wardhaugh/clojure/pom.xml b/challenge-096/tyler-wardhaugh/clojure/pom.xml index 92317a848c..04154a698d 100644 --- a/challenge-096/tyler-wardhaugh/clojure/pom.xml +++ b/challenge-096/tyler-wardhaugh/clojure/pom.xml @@ -2,11 +2,11 @@ 4.0.0 tw.weekly - tw.weekly.c94 + tw.weekly.c96 0.1.0-SNAPSHOT - tw.weekly.c94 - Challenge #094 - https://github.com/tw.weekly/tw.weekly.c94 + tw.weekly.c96 + Challenge #096 + https://github.com/tw.weekly/tw.weekly.c96 Eclipse Public License @@ -19,9 +19,9 @@ - https://github.com/tw.weekly/tw.weekly.c94 - scm:git:git://github.com/tw.weekly/tw.weekly.c94.git - scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c94.git + https://github.com/tw.weekly/tw.weekly.c96 + scm:git:git://github.com/tw.weekly/tw.weekly.c96.git + scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c96.git HEAD diff --git a/challenge-096/tyler-wardhaugh/clojure/src/tw/weekly/c96/core.clj b/challenge-096/tyler-wardhaugh/clojure/src/tw/weekly/c96/core.clj new file mode 100644 index 0000000000..63aeb35497 --- /dev/null +++ b/challenge-096/tyler-wardhaugh/clojure/src/tw/weekly/c96/core.clj @@ -0,0 +1,12 @@ +(ns tw.weekly.c96.core + (:require [tw.weekly.c96.t1 :as t1]) + (:require [tw.weekly.c96.t2 :as t2]) + (:gen-class)) + +(defn -main + "Run all tasks" + [& _] + (println "Task #1:") + (t1/-main) + (println "\nTask #2:") + (t2/-main)) diff --git a/challenge-096/tyler-wardhaugh/clojure/src/tw/weekly/c96/t1.clj b/challenge-096/tyler-wardhaugh/clojure/src/tw/weekly/c96/t1.clj new file mode 100644 index 0000000000..9d5ab76cb5 --- /dev/null +++ b/challenge-096/tyler-wardhaugh/clojure/src/tw/weekly/c96/t1.clj @@ -0,0 +1,25 @@ +(ns tw.weekly.c96.t1 + (:require [clojure.edn :as edn] + [clojure.string :as str])) + +;;; +; Task description for TASK #1 › Reverse Words +;;; + +(def DEFAULT-INPUT "The Weekly Challenge") + +(defn reverse-words + "Split a string by spaces and reassemble in reverse word order." + [s] + (-> s + str/trim + (str/split #"\s+") + reverse + (->> (str/join " ")))) + +(defn -main + "Run Task 1 using a string S, defaulting to the example given in + the task description." + [& args] + (let [S (or (some-> args first edn/read-string) DEFAULT-INPUT)] + (println (reverse-words S)))) diff --git a/challenge-096/tyler-wardhaugh/clojure/src/tw/weekly/c96/t2.clj b/challenge-096/tyler-wardhaugh/clojure/src/tw/weekly/c96/t2.clj new file mode 100644 index 0000000000..95e3d9d76e --- /dev/null +++ b/challenge-096/tyler-wardhaugh/clojure/src/tw/weekly/c96/t2.clj @@ -0,0 +1,31 @@ +(ns tw.weekly.c96.t2 + (:require [clojure.edn :as edn])) + +;;; +; Task description for TASK #2 › Edit Distance +;;; + +(def DEFAULT-INPUT ["kitten" "sitting"]) + +(defn edit-distance + "Compute the minimum operations required to convert S1 to S2." + [s1 s2] + (let [ed (fn [f [h1 & t1 :as s1] [h2 & t2 :as s2]] + (cond + (empty? s1) (count s2) + (empty? s2) (count s1) + (= h1 h2) (f f t1 t2) + :else (->> [[t1 s2] + [s1 t2] + [t1 t2]] + (transduce (map (partial apply f f)) min ##Inf) + inc))) + ed' (memoize ed)] + (ed' ed' (seq s1) (seq s2)))) + +(defn -main + "Run Task 2 using strings S1 and S2, defaulting to the example given in + the task description." + [& args] + (let [[S1 S2] (or (some->> args (take 2) (map edn/read-string)) DEFAULT-INPUT)] + (println (edit-distance S1 S2)))) diff --git a/challenge-096/tyler-wardhaugh/clojure/test/tw/weekly/c96_test.clj b/challenge-096/tyler-wardhaugh/clojure/test/tw/weekly/c96_test.clj new file mode 100644 index 0000000000..1a3eb10dfe --- /dev/null +++ b/challenge-096/tyler-wardhaugh/clojure/test/tw/weekly/c96_test.clj @@ -0,0 +1,16 @@ +(ns tw.weekly.c96-test + (:require [clojure.test :refer [deftest is testing]] + [tw.weekly.c96.t1 :refer [reverse-words]] + [tw.weekly.c96.t2 :refer [edit-distance]])) + +(deftest task-1 + (testing "Task 1, Reverse Words" + (is (= "Challenge Weekly The" (reverse-words "The Weekly Challenge"))) + (is (= "family same the of part are Raku and Perl" + (reverse-words + " Perl and Raku are part of the same family "))))) + +(deftest task-2 + (testing "Task 2, Edit Distance" + (is (= 3 (edit-distance "kitten" "sitting"))) + (is (= 2 (edit-distance "sunday" "monday"))))) -- cgit