diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-02 11:18:52 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-02 11:18:52 +0000 |
| commit | 1e83935def2ee773eab1adfc0be4fd404472e9c5 (patch) | |
| tree | 9fd67e54a2a05293e8f589011637cd515506b0e5 | |
| parent | ca2a93e05c838ef0bdd86b01c8def081c1586088 (diff) | |
| parent | 903dbdf51d3c296b101177a6f574d1e02c439f61 (diff) | |
| download | perlweeklychallenge-club-1e83935def2ee773eab1adfc0be4fd404472e9c5.tar.gz perlweeklychallenge-club-1e83935def2ee773eab1adfc0be4fd404472e9c5.tar.bz2 perlweeklychallenge-club-1e83935def2ee773eab1adfc0be4fd404472e9c5.zip | |
Merge pull request #9336 from tylerw/tw/challenge-250
Ch250: Implement Tasks 1 & 2 in Clojure
8 files changed, 84 insertions, 8 deletions
diff --git a/challenge-250/tyler-wardhaugh/clojure/README.md b/challenge-250/tyler-wardhaugh/clojure/README.md index c8680fc4e9..7f4baaeac2 100644 --- a/challenge-250/tyler-wardhaugh/clojure/README.md +++ b/challenge-250/tyler-wardhaugh/clojure/README.md @@ -1,6 +1,6 @@ -# c233 +# c250 -The Weekly Challenge — #233 — Tyler Wardhaugh +The Weekly Challenge — #250 — Tyler Wardhaugh ## Usage @@ -36,6 +36,6 @@ View available tasks Babashka can run: ## License -Copyright © 2023 Tyler Wardhaugh +Copyright © 2024 Tyler Wardhaugh Distributed under the Eclipse Public License version 1.0. diff --git a/challenge-250/tyler-wardhaugh/clojure/bb.edn b/challenge-250/tyler-wardhaugh/clojure/bb.edn index fff147cbd8..a2208df1e7 100644 --- a/challenge-250/tyler-wardhaugh/clojure/bb.edn +++ b/challenge-250/tyler-wardhaugh/clojure/bb.edn @@ -1,6 +1,6 @@ { :paths ["src" "resources"] - :deps {c233/c233 {:local/root "."}} + :deps {c250/c250 {:local/root "."}} :tasks { diff --git a/challenge-250/tyler-wardhaugh/clojure/build.clj b/challenge-250/tyler-wardhaugh/clojure/build.clj new file mode 100644 index 0000000000..62a880d1ea --- /dev/null +++ b/challenge-250/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.c250/c250) +(def version "0.1.0-SNAPSHOT") +(def main 'c250.c250) + +(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-250/tyler-wardhaugh/clojure/deps.edn b/challenge-250/tyler-wardhaugh/clojure/deps.edn index d185d4a1ee..6dc182b2bc 100644 --- a/challenge-250/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-250/tyler-wardhaugh/clojure/deps.edn @@ -1,9 +1,8 @@ {:paths ["src" "resources"] - :deps {org.clojure/clojure {:mvn/version "1.11.1"} - net.cgrand/xforms {:mvn/version "0.19.5"}} + :deps {org.clojure/clojure {:mvn/version "1.11.1"}} :aliases - {:t1 {:main-opts ["-m" "c233.t1"]} - :t2 {:main-opts ["-m" "c233.t2"]} + {:t1 {:main-opts ["-m" "c250.t1"]} + :t2 {:main-opts ["-m" "c250.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-250/tyler-wardhaugh/clojure/src/c250/t1.clj b/challenge-250/tyler-wardhaugh/clojure/src/c250/t1.clj new file mode 100644 index 0000000000..c6a12f88b5 --- /dev/null +++ b/challenge-250/tyler-wardhaugh/clojure/src/c250/t1.clj @@ -0,0 +1,20 @@ +(ns c250.t1 + (:require + [clojure.edn :as edn])) + +(def DEFAULT-INPUT [[0 1 2]]) + +(defn smallest-index + [coll] + (or + (->> coll + (keep-indexed (fn [i v] (when (== v (mod i 10)) i))) + first) + -1)) + +(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 (smallest-index COLL)))) diff --git a/challenge-250/tyler-wardhaugh/clojure/src/c250/t2.clj b/challenge-250/tyler-wardhaugh/clojure/src/c250/t2.clj new file mode 100644 index 0000000000..1f18743b3f --- /dev/null +++ b/challenge-250/tyler-wardhaugh/clojure/src/c250/t2.clj @@ -0,0 +1,21 @@ +(ns c250.t2 + (:require + [clojure.edn :as edn])) + +(def DEFAULT-INPUT [["perl", "2", "000", "python", "r4ku"]]) + +(defn alphanumeric-string + [coll] + (let [xf (map (fn [s-or-int] + (if-let [i (parse-long s-or-int)] + i + (count s-or-int)))) + f (completing (fn [w v] (max w v)))] + (transduce xf f ##-Inf coll))) + +(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)] + (println (alphanumeric-string COLL)))) diff --git a/challenge-250/tyler-wardhaugh/clojure/test/c250/t1_test.clj b/challenge-250/tyler-wardhaugh/clojure/test/c250/t1_test.clj new file mode 100644 index 0000000000..08205c61c2 --- /dev/null +++ b/challenge-250/tyler-wardhaugh/clojure/test/c250/t1_test.clj @@ -0,0 +1,9 @@ +(ns c250.t1-test + (:require [clojure.test :refer [deftest is testing]] + [c250.t1 :refer [smallest-index]])) + +(deftest task-1 + (testing "Task 1 produces the correct results from examples in the description" + (is (= 0 (smallest-index [0 1 2]))) + (is (= 2 (smallest-index [4 3 2 1]))) + (is (= -1 (smallest-index [1 2 3 4 5 6 7 8 9 0]))))) diff --git a/challenge-250/tyler-wardhaugh/clojure/test/c250/t2_test.clj b/challenge-250/tyler-wardhaugh/clojure/test/c250/t2_test.clj new file mode 100644 index 0000000000..ab188274fc --- /dev/null +++ b/challenge-250/tyler-wardhaugh/clojure/test/c250/t2_test.clj @@ -0,0 +1,8 @@ +(ns c250.t2-test + (:require [clojure.test :refer [deftest is testing]] + [c250.t2 :refer [alphanumeric-string]])) + +(deftest task-1 + (testing "Task 2 produces the correct results from examples in the description" + (is (= 6 (alphanumeric-string ["perl" "2" "000" "python" "r4ku"]))) + (is (= 1 (alphanumeric-string ["001" "1" "000" "0001"]))))) |
