diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-07-09 19:38:44 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-09 19:38:44 +0100 |
| commit | 906d4faf6aabd03036a58f889a74341bd283d402 (patch) | |
| tree | 6bbce44f9be0f25d6ef2a232b9b9be4354b209b4 | |
| parent | 0283f03dc09b9bed0ee69842cdbd4c3f6ed278cc (diff) | |
| parent | e19458d872d59af898b1dd172b62b03bfe784ddc (diff) | |
| download | perlweeklychallenge-club-906d4faf6aabd03036a58f889a74341bd283d402.tar.gz perlweeklychallenge-club-906d4faf6aabd03036a58f889a74341bd283d402.tar.bz2 perlweeklychallenge-club-906d4faf6aabd03036a58f889a74341bd283d402.zip | |
Merge pull request #8337 from tylerw/tw/challenge-224
Ch224: implement Tasks 1 & 2 in Clojure
8 files changed, 107 insertions, 13 deletions
diff --git a/challenge-224/tyler-wardhaugh/clojure/README.md b/challenge-224/tyler-wardhaugh/clojure/README.md index 7b86c0068b..1fc0d33f84 100644 --- a/challenge-224/tyler-wardhaugh/clojure/README.md +++ b/challenge-224/tyler-wardhaugh/clojure/README.md @@ -1,6 +1,6 @@ -# c223 +# c224 -The Weekly Challenge — #223 — Tyler Wardhaugh +The Weekly Challenge — #224 — Tyler Wardhaugh ## Usage @@ -8,18 +8,21 @@ Clojure ([installation instructions](https://clojure.org/guides/getting_started# Run Task #1: - $ clojure -M:t1 N + $ clojure -M:t1 SOURCE TARGET # ... or ... - $ bb run task-1 N + $ bb run task-1 SOURCE TARGET + + # Alternatively, to run it via Babashka: + $ bb run task-1-bb SOURCE TARGET Run Task #2: - $ clojure -M:t2 COLL + $ clojure -M:t2 N # ... or ... - $ bb run task-2 COLL + $ bb run task-2 N # Alternatively, to run it via Babashka: - $ bb run task-2-bb COLL + $ bb run task-2-bb N Run the project's tests (which are samples from the task descriptions): diff --git a/challenge-224/tyler-wardhaugh/clojure/bb.edn b/challenge-224/tyler-wardhaugh/clojure/bb.edn index 09ef39da1e..3c885d11a6 100644 --- a/challenge-224/tyler-wardhaugh/clojure/bb.edn +++ b/challenge-224/tyler-wardhaugh/clojure/bb.edn @@ -1,6 +1,6 @@ { :paths ["src" "resources"] - :deps {c223/c223 {:local/root "."}} + :deps {c224/c224 {:local/root "."}} :tasks { diff --git a/challenge-224/tyler-wardhaugh/clojure/build.clj b/challenge-224/tyler-wardhaugh/clojure/build.clj new file mode 100644 index 0000000000..67267daa7d --- /dev/null +++ b/challenge-224/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.c224/c224) +(def version "0.1.0-SNAPSHOT") +(def main 'c224.c224) + +(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-224/tyler-wardhaugh/clojure/deps.edn b/challenge-224/tyler-wardhaugh/clojure/deps.edn index f3cec366e5..ddb4ddaca3 100644 --- a/challenge-224/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-224/tyler-wardhaugh/clojure/deps.edn @@ -1,12 +1,9 @@ {:paths ["src" "resources"] :deps {org.clojure/clojure {:mvn/version "1.11.1"} - clojure/math.numeric-tower {:git/url "https://github.com/clojure/math.numeric-tower" - :git/sha "3e98b31da229d7d3a533f1cee0c509e9b349aa17"} - com.hypirion/primes {:mvn/version "0.2.2"} net.cgrand/xforms {:mvn/version "0.19.4"}} :aliases - {:t1 {:main-opts ["-m" "c223.t1"]} - :t2 {:main-opts ["-m" "c223.t2"]} + {:t1 {:main-opts ["-m" "c224.t1"]} + :t2 {:main-opts ["-m" "c224.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-224/tyler-wardhaugh/clojure/src/c224/t1.clj b/challenge-224/tyler-wardhaugh/clojure/src/c224/t1.clj new file mode 100644 index 0000000000..ea41f23a17 --- /dev/null +++ b/challenge-224/tyler-wardhaugh/clojure/src/c224/t1.clj @@ -0,0 +1,18 @@ +(ns c224.t1) + +(def DEFAULT-INPUT ["abc" "xyz"]) + +(defn special-notes? + [source target] + (let [s (-> source frequencies) + t (-> target frequencies (update-vals -))] + (->> (merge-with + s t) + vals + (every? (complement neg?))))) + +(defn -main + "Run Task 1 with a given input N, defaulting to the first example from the + task description." + [& args] + (let [[SOURCE TARGET] (or args DEFAULT-INPUT)] + (println (if (special-notes? SOURCE TARGET) "true" "false")))) diff --git a/challenge-224/tyler-wardhaugh/clojure/src/c224/t2.clj b/challenge-224/tyler-wardhaugh/clojure/src/c224/t2.clj new file mode 100644 index 0000000000..ac50b8cb16 --- /dev/null +++ b/challenge-224/tyler-wardhaugh/clojure/src/c224/t2.clj @@ -0,0 +1,39 @@ +(ns c224.t2 + (:require + [clojure.edn :as edn] + [net.cgrand.xforms :as x])) + +(def DEFAULT-INPUT ["112358"]) + +(defn coll->n + [coll] + (->> coll (x/str identity) parse-long)) + +(defn additive-number? + [s] + (let [sv (into [] (map #(Character/digit % 10)) s) + len (count sv) + xf (x/for [i % + j (range 1 (- len i)) + :let [n1 (-> sv (subvec 0 i) coll->n) + n2 (-> sv (subvec i (+ i j)) coll->n) + rsv (-> sv (subvec (+ i j) len))]] + [n1 n2 rsv]) + f (fn f [_ [n1 n2 rsv]] + (let [sum (+ n1 n2) + sumlen (-> sum str count)] + (when (<= sumlen (count rsv)) + (let [n3 (-> rsv (subvec 0 sumlen) coll->n) + rrsv (-> rsv (subvec sumlen (count rsv)))] + (when (= sum n3) + (if (zero? (count rrsv)) + (reduced true) + (f nil [n2 n3 rrsv])))))))] + (boolean (transduce xf (completing f) nil (range 1 len))))) + +(defn -main + "Run Task 2 with a given input COLL, defaulting to the first example from the + task description." + [& args] + (let [[N] (or args DEFAULT-INPUT)] + (println (if (additive-number? N) "true" "false")))) diff --git a/challenge-224/tyler-wardhaugh/clojure/test/c224/t1_test.clj b/challenge-224/tyler-wardhaugh/clojure/test/c224/t1_test.clj new file mode 100644 index 0000000000..c64576d21d --- /dev/null +++ b/challenge-224/tyler-wardhaugh/clojure/test/c224/t1_test.clj @@ -0,0 +1,9 @@ +(ns c224.t1-test + (:require [clojure.test :refer [deftest is testing]] + [c224.t1 :refer [special-notes?]])) + +(deftest task-1 + (testing "Task 1 produces the correct results from examples in the description" + (is (false? (special-notes? "abc" "xyz"))) + (is (true? (special-notes? "scriptinglanguage" "perl"))) + (is (true? (special-notes? "aabbcc" "abc"))))) diff --git a/challenge-224/tyler-wardhaugh/clojure/test/c224/t2_test.clj b/challenge-224/tyler-wardhaugh/clojure/test/c224/t2_test.clj new file mode 100644 index 0000000000..af887c72f3 --- /dev/null +++ b/challenge-224/tyler-wardhaugh/clojure/test/c224/t2_test.clj @@ -0,0 +1,9 @@ +(ns c224.t2-test + (:require [clojure.test :refer [deftest is testing]] + [c224.t2 :refer [additive-number?]])) + +(deftest task-1 + (testing "Task 2 produces the correct results from examples in the description" + (is (true? (additive-number? "112358"))) + (is (false? (additive-number? "12345"))) + (is (true? (additive-number? "199100199"))))) |
