diff options
| author | Tyler Wardhaugh <tyler.wardhaugh@gmail.com> | 2023-03-25 10:40:48 -0700 |
|---|---|---|
| committer | Tyler Wardhaugh <tyler.wardhaugh@gmail.com> | 2023-03-25 10:43:47 -0700 |
| commit | 4e305ff7ecd05c9d22adbc941cd77026cdd6fdae (patch) | |
| tree | 0e977bf6d624fa7f3a50362732b3509bea520601 | |
| parent | 9aad65d98804be4c3723443b7c80f67b4ff373aa (diff) | |
| download | perlweeklychallenge-club-4e305ff7ecd05c9d22adbc941cd77026cdd6fdae.tar.gz perlweeklychallenge-club-4e305ff7ecd05c9d22adbc941cd77026cdd6fdae.tar.bz2 perlweeklychallenge-club-4e305ff7ecd05c9d22adbc941cd77026cdd6fdae.zip | |
Ch209: solve Tasks 1 & 2 in Clojure
8 files changed, 91 insertions, 5 deletions
diff --git a/challenge-209/tyler-wardhaugh/clojure/README.md b/challenge-209/tyler-wardhaugh/clojure/README.md index b7d0abb1ee..acccb9d7f1 100644 --- a/challenge-209/tyler-wardhaugh/clojure/README.md +++ b/challenge-209/tyler-wardhaugh/clojure/README.md @@ -1,6 +1,6 @@ -# c205 +# c209 -The Weekly Challenge — #205 — Tyler Wardhaugh +The Weekly Challenge — #209 — Tyler Wardhaugh ## Usage diff --git a/challenge-209/tyler-wardhaugh/clojure/bb.edn b/challenge-209/tyler-wardhaugh/clojure/bb.edn index 46c9cae858..7acc226111 100644 --- a/challenge-209/tyler-wardhaugh/clojure/bb.edn +++ b/challenge-209/tyler-wardhaugh/clojure/bb.edn @@ -1,6 +1,6 @@ { :paths ["src" "resources"] - :deps {c205/c205 {:local/root "."}} + :deps {c209/c209 {:local/root "."}} :tasks { diff --git a/challenge-209/tyler-wardhaugh/clojure/build.clj b/challenge-209/tyler-wardhaugh/clojure/build.clj new file mode 100644 index 0000000000..0db61aefe6 --- /dev/null +++ b/challenge-209/tyler-wardhaugh/clojure/build.clj @@ -0,0 +1,19 @@ +(ns build + (:refer-clojure :exclude [test]) + (:require [org.corfield.build :as bb])) + +(def lib 'net.clojars.c209/c209) +(def version "0.1.0-SNAPSHOT") +(def main 'c209.c209) + +(defn test "Run the tests." [opts] + (bb/run-tests opts)) + +(def clean bb/clean) + +(defn ci "Run the CI pipeline of tests (and build the uberjar)." [opts] + (-> opts + (assoc :lib lib :version version :main main) + (bb/run-tests) + (bb/clean) + (bb/uber))) diff --git a/challenge-209/tyler-wardhaugh/clojure/deps.edn b/challenge-209/tyler-wardhaugh/clojure/deps.edn index 619d09fa32..74c8d4ec8e 100644 --- a/challenge-209/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-209/tyler-wardhaugh/clojure/deps.edn @@ -1,8 +1,8 @@ {:paths ["src" "resources"] :deps {org.clojure/clojure {:mvn/version "1.11.1"}} :aliases - {:t1 {:main-opts ["-m" "c205.t1"]} - :t2 {:main-opts ["-m" "c205.t2"]} + {:t1 {:main-opts ["-m" "c209.t1"]} + :t2 {:main-opts ["-m" "c209.t2"]} :build {:deps {io.github.seancorfield/build-clj {:git/tag "v0.8.3" :git/sha "7ac1f8d" ;; since we're building an app uberjar, we do not diff --git a/challenge-209/tyler-wardhaugh/clojure/src/c209/t1.clj b/challenge-209/tyler-wardhaugh/clojure/src/c209/t1.clj new file mode 100644 index 0000000000..9db313a2d9 --- /dev/null +++ b/challenge-209/tyler-wardhaugh/clojure/src/c209/t1.clj @@ -0,0 +1,16 @@ +(ns c209.t1 + (:require + [clojure.edn :as edn])) + +(def DEFAULT-INPUT [[1 0 0]]) + +(defn ends-with-a? + [coll] + (and (odd? (count coll)) (zero? (last coll)))) + +(defn -main + "Run Task 1 with a given input COLL, defaulting to the first example from + the task description." + [& args] + (let [[coll] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)] + (println (if (ends-with-a? coll) 1 0)))) diff --git a/challenge-209/tyler-wardhaugh/clojure/src/c209/t2.clj b/challenge-209/tyler-wardhaugh/clojure/src/c209/t2.clj new file mode 100644 index 0000000000..3dbb4a6825 --- /dev/null +++ b/challenge-209/tyler-wardhaugh/clojure/src/c209/t2.clj @@ -0,0 +1,25 @@ +(ns c209.t2 + (:require + [clojure.edn :as edn] + [clojure.set :as set])) + +(def DEFAULT-INPUT [[["A" "a1@a.com" "a2@a.com"] + ["B" "b1@b.com"] + ["A" "a3@a.com" "a1@a.com"]]]) + +(defn merge-accounts + [accounts] + (->> accounts + (group-by first) + (mapcat (fn [[k vv]] + (let [sets (map (comp set rest) vv)] + (if (seq (apply set/intersection sets)) + [(into [k] (sort (apply set/union sets)))] + vv)))))) + +(defn -main + "Run Task 2 with a given input COLL, defaulting to the first + example from the task description." + [& args] + (let [[coll] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)] + (prn (merge-accounts coll)))) diff --git a/challenge-209/tyler-wardhaugh/clojure/test/c209/t1_test.clj b/challenge-209/tyler-wardhaugh/clojure/test/c209/t1_test.clj new file mode 100644 index 0000000000..c8adde1444 --- /dev/null +++ b/challenge-209/tyler-wardhaugh/clojure/test/c209/t1_test.clj @@ -0,0 +1,8 @@ +(ns c209.t1-test + (:require [clojure.test :refer [deftest is testing]] + [c209.t1 :refer [ends-with-a?]])) + +(deftest task-1 + (testing "Task 1 produces the correct results from examples in the description" + (is (true? (ends-with-a? [1 0 0]))) + (is (false? (ends-with-a? [1 1 1 0]))))) diff --git a/challenge-209/tyler-wardhaugh/clojure/test/c209/t2_test.clj b/challenge-209/tyler-wardhaugh/clojure/test/c209/t2_test.clj new file mode 100644 index 0000000000..c690478116 --- /dev/null +++ b/challenge-209/tyler-wardhaugh/clojure/test/c209/t2_test.clj @@ -0,0 +1,18 @@ +(ns c209.t2-test + (:require [clojure.test :refer [deftest is testing]] + [c209.t2 :refer [merge-accounts]])) + +(deftest task-2 + (testing "Task 2 produces the correct results from examples in the description" + (is (= (merge-accounts [["A" "a1@a.com" "a2@a.com"] + ["B" "b1@b.com"] + ["A" "a3@a.com" "a1@a.com"]]) + [["A" "a1@a.com" "a2@a.com" "a3@a.com"] + ["B" "b1@b.com"]])) + (is (= (merge-accounts [["A" "a1@a.com" "a2@a.com"] + ["B" "b1@b.com"] + ["A" "a3@a.com"] + ["B" "b2@b.com" "b1@b.com"]]) + [["A" "a1@a.com" "a2@a.com"] + ["A" "a3@a.com"] + ["B" "b1@b.com" "b2@b.com"]])))) |
