diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-05-21 19:47:42 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-21 19:47:42 +0100 |
| commit | de04db6ecfe4a9bc902a23e8d8b3120616a1c0c5 (patch) | |
| tree | bda72867fa6bd9ee9c567b3584d42a3db8964134 | |
| parent | 87fa59aa409fc5ff116dac0c8433b56c47168ef3 (diff) | |
| parent | 5afb4e5aa13acc773018fdbb7a694a36a6d24fb9 (diff) | |
| download | perlweeklychallenge-club-de04db6ecfe4a9bc902a23e8d8b3120616a1c0c5.tar.gz perlweeklychallenge-club-de04db6ecfe4a9bc902a23e8d8b3120616a1c0c5.tar.bz2 perlweeklychallenge-club-de04db6ecfe4a9bc902a23e8d8b3120616a1c0c5.zip | |
Merge pull request #8111 from tylerw/tw/challenge-217
Ch217: implement Tasks 1 & 2 in Clojure
8 files changed, 85 insertions, 11 deletions
diff --git a/challenge-217/tyler-wardhaugh/clojure/README.md b/challenge-217/tyler-wardhaugh/clojure/README.md index 530c7479c2..edcc7f57c7 100644 --- a/challenge-217/tyler-wardhaugh/clojure/README.md +++ b/challenge-217/tyler-wardhaugh/clojure/README.md @@ -1,6 +1,6 @@ -# c216 +# c217 -The Weekly Challenge — #216 — Tyler Wardhaugh +The Weekly Challenge — #217 — Tyler Wardhaugh ## Usage @@ -8,21 +8,21 @@ Clojure ([installation instructions](https://clojure.org/guides/getting_started# Run Task #1: - $ clojure -M:t1 REG COLL + $ clojure -M:t1 COLL # ... or ... - $ bb run task-1 REG COLL + $ bb run task-1 COLL # Alternatively, to run it via Babashka: - $ bb run task-1-bb REG COLL + $ bb run task-1-bb COLL Run Task #2: - $ clojure -M:t2 WORD COLL + $ clojure -M:t2 COLL # ... or ... - $ bb run task-2 WORD COLL + $ bb run task-2 COLL # Alternatively, to run it via Babashka: - $ bb run task-2-bb WORD COLL + $ bb run task-2-bb COLL Run the project's tests (which are samples from the task descriptions): diff --git a/challenge-217/tyler-wardhaugh/clojure/bb.edn b/challenge-217/tyler-wardhaugh/clojure/bb.edn index f62b373e9f..e9f441cb63 100644 --- a/challenge-217/tyler-wardhaugh/clojure/bb.edn +++ b/challenge-217/tyler-wardhaugh/clojure/bb.edn @@ -1,6 +1,6 @@ { :paths ["src" "resources"] - :deps {c216/c216 {:local/root "."}} + :deps {c217/c217 {:local/root "."}} :tasks { diff --git a/challenge-217/tyler-wardhaugh/clojure/build.clj b/challenge-217/tyler-wardhaugh/clojure/build.clj new file mode 100644 index 0000000000..18baef6c0c --- /dev/null +++ b/challenge-217/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.c217/c217) +(def version "0.1.0-SNAPSHOT") +(def main 'c217.c217) + +(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-217/tyler-wardhaugh/clojure/deps.edn b/challenge-217/tyler-wardhaugh/clojure/deps.edn index 0459e1f12d..9c7a50a024 100644 --- a/challenge-217/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-217/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" "c216.t1"]} - :t2 {:main-opts ["-m" "c216.t2"]} + {:t1 {:main-opts ["-m" "c217.t1"]} + :t2 {:main-opts ["-m" "c217.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-217/tyler-wardhaugh/clojure/src/c217/t1.clj b/challenge-217/tyler-wardhaugh/clojure/src/c217/t1.clj new file mode 100644 index 0000000000..6eb9e7e85d --- /dev/null +++ b/challenge-217/tyler-wardhaugh/clojure/src/c217/t1.clj @@ -0,0 +1,17 @@ +(ns c217.t1 + (:require + [clojure.edn :as edn])) + +(def DEFAULT-INPUT [[[3 1 2] [5 2 4] [0 1 3]]]) + +(defn third-smallest-of-sorted-matrix + [coll] + (let [sorted (->> coll (mapcat identity) sort)] + (nth sorted 2))) + +(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 (third-smallest-of-sorted-matrix coll)))) diff --git a/challenge-217/tyler-wardhaugh/clojure/src/c217/t2.clj b/challenge-217/tyler-wardhaugh/clojure/src/c217/t2.clj new file mode 100644 index 0000000000..ed72f558b2 --- /dev/null +++ b/challenge-217/tyler-wardhaugh/clojure/src/c217/t2.clj @@ -0,0 +1,18 @@ +(ns c217.t2 + (:require + [clojure.edn :as edn] + [clojure.string :as str])) + +(def DEFAULT-INPUT [[1 23]]) + +(defn max-number + [coll] + (let [cmp (fn [a b] (compare (str b a) (str a b)))] + (->> coll (sort cmp) str/join parse-long))) + +(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 (max-number coll)))) diff --git a/challenge-217/tyler-wardhaugh/clojure/test/c217/t1_test.clj b/challenge-217/tyler-wardhaugh/clojure/test/c217/t1_test.clj new file mode 100644 index 0000000000..e95fe74789 --- /dev/null +++ b/challenge-217/tyler-wardhaugh/clojure/test/c217/t1_test.clj @@ -0,0 +1,9 @@ +(ns c217.t1-test + (:require [clojure.test :refer [deftest is testing]] + [c217.t1 :refer [third-smallest-of-sorted-matrix]])) + +(deftest task-1 + (testing "Task 1 produces the correct results from examples in the description" + (is (= 1 (third-smallest-of-sorted-matrix [[3 1 2] [5 2 4] [0 1 3]]))) + (is (= 4 (third-smallest-of-sorted-matrix [[2 1] [4 5]]))) + (is (= 0 (third-smallest-of-sorted-matrix [[1 0 3] [0 0 0] [1 2 1]]))))) diff --git a/challenge-217/tyler-wardhaugh/clojure/test/c217/t2_test.clj b/challenge-217/tyler-wardhaugh/clojure/test/c217/t2_test.clj new file mode 100644 index 0000000000..705d631493 --- /dev/null +++ b/challenge-217/tyler-wardhaugh/clojure/test/c217/t2_test.clj @@ -0,0 +1,11 @@ +(ns c217.t2-test + (:require [clojure.test :refer [deftest is testing]] + [c217.t2 :refer [max-number]])) + +(deftest task-2 + (testing "Task 2 produces the correct results from examples in the description" + (is (= 231 (max-number [1 23]))) + (is (= 3210 (max-number [10 3 2]))) + (is (= 431210 (max-number [31 2 4 10]))) + (is (= 542111 (max-number [5 11 4 1 2]))) + (is (= 110 (max-number [1 10]))))) |
