diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-01-15 21:44:42 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-15 21:44:42 +0000 |
| commit | 809b5857766d121a2a41088d4eacbaa1e0eebb86 (patch) | |
| tree | ff30117a92bb0dd586fac482a91f868d5db263f1 | |
| parent | f8199bdb99771c1f524b7b1e5e3abe8f0ae0ebc7 (diff) | |
| parent | c158f0cd1099e0f75510dd8f3e9dc0f733320cde (diff) | |
| download | perlweeklychallenge-club-809b5857766d121a2a41088d4eacbaa1e0eebb86.tar.gz perlweeklychallenge-club-809b5857766d121a2a41088d4eacbaa1e0eebb86.tar.bz2 perlweeklychallenge-club-809b5857766d121a2a41088d4eacbaa1e0eebb86.zip | |
Merge pull request #7412 from tylerw/tw/challenge-199
Ch199: solve Tasks 1 & 2 in Clojure
8 files changed, 87 insertions, 7 deletions
diff --git a/challenge-199/tyler-wardhaugh/clojure/README.md b/challenge-199/tyler-wardhaugh/clojure/README.md index c89eb6b103..23b26176e1 100644 --- a/challenge-199/tyler-wardhaugh/clojure/README.md +++ b/challenge-199/tyler-wardhaugh/clojure/README.md @@ -1,6 +1,6 @@ -# c198 +# c199 -The Weekly Challenge — #198 — Tyler Wardhaugh +The Weekly Challenge — #199 — Tyler Wardhaugh ## Usage @@ -17,7 +17,7 @@ Run Task #1: Run Task #2: - $ clojure -M:t2 COLL + $ clojure -M:t2 X Y Z COLL # ... or ... $ bb run task-2 COLL @@ -36,6 +36,6 @@ View available tasks Babashka can run: ## License -Copyright © 2022 Tyler Wardhaugh +Copyright © 2023 Tyler Wardhaugh Distributed under the Eclipse Public License version 1.0. diff --git a/challenge-199/tyler-wardhaugh/clojure/bb.edn b/challenge-199/tyler-wardhaugh/clojure/bb.edn index 5709814635..4d2688a77c 100644 --- a/challenge-199/tyler-wardhaugh/clojure/bb.edn +++ b/challenge-199/tyler-wardhaugh/clojure/bb.edn @@ -1,6 +1,6 @@ { :paths ["src" "resources"] - :deps {c198/c198 {:local/root "."}} + :deps {c199/c199 {:local/root "."}} :tasks { diff --git a/challenge-199/tyler-wardhaugh/clojure/build.clj b/challenge-199/tyler-wardhaugh/clojure/build.clj new file mode 100644 index 0000000000..1e3dca8147 --- /dev/null +++ b/challenge-199/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.c199/c199) +(def version "0.1.0-SNAPSHOT") +(def main 'c199.c199) + +(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-199/tyler-wardhaugh/clojure/deps.edn b/challenge-199/tyler-wardhaugh/clojure/deps.edn index 7ccdfdb16c..e09444c323 100644 --- a/challenge-199/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-199/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" "c198.t1"]} - :t2 {:main-opts ["-m" "c198.t2"]} + {:t1 {:main-opts ["-m" "c199.t1"]} + :t2 {:main-opts ["-m" "c199.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-199/tyler-wardhaugh/clojure/src/c199/t1.clj b/challenge-199/tyler-wardhaugh/clojure/src/c199/t1.clj new file mode 100644 index 0000000000..607de01a06 --- /dev/null +++ b/challenge-199/tyler-wardhaugh/clojure/src/c199/t1.clj @@ -0,0 +1,18 @@ +(ns c199.t1 + (:require + [clojure.edn :as edn])) + +(def DEFAULT-INPUT [[1 2 3 1 1 3]]) + +(defn good-pairs + [coll] + (let [source (->> coll frequencies vals) + xf (keep (fn [n] (when (< 1 n) (quot (* n (dec n)) 2))))] + (transduce xf + 0 source))) + +(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 (good-pairs coll)))) diff --git a/challenge-199/tyler-wardhaugh/clojure/src/c199/t2.clj b/challenge-199/tyler-wardhaugh/clojure/src/c199/t2.clj new file mode 100644 index 0000000000..29c535f571 --- /dev/null +++ b/challenge-199/tyler-wardhaugh/clojure/src/c199/t2.clj @@ -0,0 +1,26 @@ +(ns c199.t2 + (:require + [clojure.edn :as edn])) + +(def DEFAULT-INPUT [7 2 3 [3 0 1 1 9 7]]) + +(defn good-triplets + [x y z coll] + (let [indexed (map-indexed vector coll)] + (->> (for [[i ai] indexed + [j aj] (drop 1 indexed) + :when (and (< i j) + (<= (abs (- ai aj)) x)) + [k ak] (drop 2 indexed) + :when (and (< j k) + (<= (abs (- aj ak)) y) + (<= (abs (- ai ak)) z))] + 1) + (reduce + 0)))) + +(defn -main + "Run Task 2 with a given input X, Y, Z, and COLL, defaulting to the first + example from the task description." + [& args] + (let [[x y z coll] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)] + (println (good-triplets x y z coll)))) diff --git a/challenge-199/tyler-wardhaugh/clojure/test/c199/t1_test.clj b/challenge-199/tyler-wardhaugh/clojure/test/c199/t1_test.clj new file mode 100644 index 0000000000..08b3f44c55 --- /dev/null +++ b/challenge-199/tyler-wardhaugh/clojure/test/c199/t1_test.clj @@ -0,0 +1,9 @@ +(ns c199.t1-test + (:require [clojure.test :refer [deftest is testing]] + [c199.t1 :refer [good-pairs]])) + +(deftest task-1 + (testing "Task 1 produces the correct results from examples in the description" + (is (= 4 (good-pairs [1 2 3 1 1 3]))) + (is (= 0 (good-pairs [1 2 3]))) + (is (= 6 (good-pairs [1 1 1 1]))))) diff --git a/challenge-199/tyler-wardhaugh/clojure/test/c199/t2_test.clj b/challenge-199/tyler-wardhaugh/clojure/test/c199/t2_test.clj new file mode 100644 index 0000000000..4e81d5967d --- /dev/null +++ b/challenge-199/tyler-wardhaugh/clojure/test/c199/t2_test.clj @@ -0,0 +1,8 @@ +(ns c199.t2-test + (:require [clojure.test :refer [deftest is testing]] + [c199.t2 :refer [good-triplets]])) + +(deftest task-2 + (testing "Task 2 produces the correct results from examples in the description" + (is (= 4 (good-triplets 7 2 3 [3 0 1 1 9 7]))) + (is (= 0 (good-triplets 0 0 1 [1 1 2 2 3]))))) |
