diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-02-25 19:43:03 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-25 19:43:03 +0000 |
| commit | d895a65864df6c1b2a45dcee1b93da0039aba4f1 (patch) | |
| tree | a97eb860d7516329e64123f9de959bab1ed20989 | |
| parent | f72e68cfc1dade4e359f90870235778eb6fe4234 (diff) | |
| parent | e35723eedfe0444ba4c42581fff2ccb91dec72ce (diff) | |
| download | perlweeklychallenge-club-d895a65864df6c1b2a45dcee1b93da0039aba4f1.tar.gz perlweeklychallenge-club-d895a65864df6c1b2a45dcee1b93da0039aba4f1.tar.bz2 perlweeklychallenge-club-d895a65864df6c1b2a45dcee1b93da0039aba4f1.zip | |
Merge pull request #7628 from tylerw/tw/challenge-205
Ch205: solve Tasks 1 & 2 in Clojure
8 files changed, 81 insertions, 8 deletions
diff --git a/challenge-205/tyler-wardhaugh/clojure/README.md b/challenge-205/tyler-wardhaugh/clojure/README.md index 103893d9b9..b7d0abb1ee 100644 --- a/challenge-205/tyler-wardhaugh/clojure/README.md +++ b/challenge-205/tyler-wardhaugh/clojure/README.md @@ -1,6 +1,6 @@ -# c200 +# c205 -The Weekly Challenge — #200 — Tyler Wardhaugh +The Weekly Challenge — #205 — Tyler Wardhaugh ## Usage @@ -17,12 +17,12 @@ Run Task #1: Run Task #2: - $ clojure -M:t2 N + $ clojure -M:t2 COLL # ... or ... - $ bb run task-2 N + $ bb run task-2 COLL # Alternatively, to run it via Babashka: - $ bb run task-2-bb N + $ bb run task-2-bb COLL Run the project's tests (which are samples from the task descriptions): diff --git a/challenge-205/tyler-wardhaugh/clojure/bb.edn b/challenge-205/tyler-wardhaugh/clojure/bb.edn index d7f9593e83..46c9cae858 100644 --- a/challenge-205/tyler-wardhaugh/clojure/bb.edn +++ b/challenge-205/tyler-wardhaugh/clojure/bb.edn @@ -1,6 +1,6 @@ { :paths ["src" "resources"] - :deps {c200/c200 {:local/root "."}} + :deps {c205/c205 {:local/root "."}} :tasks { diff --git a/challenge-205/tyler-wardhaugh/clojure/build.clj b/challenge-205/tyler-wardhaugh/clojure/build.clj new file mode 100644 index 0000000000..44d02594bf --- /dev/null +++ b/challenge-205/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.c205/c205) +(def version "0.1.0-SNAPSHOT") +(def main 'c205.c205) + +(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-205/tyler-wardhaugh/clojure/deps.edn b/challenge-205/tyler-wardhaugh/clojure/deps.edn index bcf2a4462e..619d09fa32 100644 --- a/challenge-205/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-205/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" "c200.t1"]} - :t2 {:main-opts ["-m" "c200.t2"]} + {:t1 {:main-opts ["-m" "c205.t1"]} + :t2 {:main-opts ["-m" "c205.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-205/tyler-wardhaugh/clojure/src/c205/t1.clj b/challenge-205/tyler-wardhaugh/clojure/src/c205/t1.clj new file mode 100644 index 0000000000..b8d66b63f8 --- /dev/null +++ b/challenge-205/tyler-wardhaugh/clojure/src/c205/t1.clj @@ -0,0 +1,17 @@ +(ns c205.t1 + (:require + [clojure.edn :as edn])) + +(def DEFAULT-INPUT [[5 4 3]]) + +(defn third-highest + [coll] + (let [sorted (->> coll (into (sorted-set-by (comp - compare))) seq)] + (nth sorted 2 (first sorted)))) + +(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-highest coll)))) diff --git a/challenge-205/tyler-wardhaugh/clojure/src/c205/t2.clj b/challenge-205/tyler-wardhaugh/clojure/src/c205/t2.clj new file mode 100644 index 0000000000..6f245b6bab --- /dev/null +++ b/challenge-205/tyler-wardhaugh/clojure/src/c205/t2.clj @@ -0,0 +1,20 @@ +(ns c205.t2 + (:require + [clojure.edn :as edn])) + +(def DEFAULT-INPUT [[1 2 3 4 5 6 7]]) + +(defn max-xor + [coll] + (let [uniq (set coll) + xors (for [x uniq + y (drop 1 uniq)] + (bit-xor x y))] + (apply max xors))) + +(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-xor coll)))) diff --git a/challenge-205/tyler-wardhaugh/clojure/test/c205/t1_test.clj b/challenge-205/tyler-wardhaugh/clojure/test/c205/t1_test.clj new file mode 100644 index 0000000000..3ab163a73e --- /dev/null +++ b/challenge-205/tyler-wardhaugh/clojure/test/c205/t1_test.clj @@ -0,0 +1,9 @@ +(ns c205.t1-test + (:require [clojure.test :refer [deftest is testing]] + [c205.t1 :refer [third-highest]])) + +(deftest task-1 + (testing "Task 1 produces the correct results from examples in the description" + (is (= 3 (third-highest [5 3 4]))) + (is (= 6 (third-highest [5 6]))) + (is (= 3 (third-highest [5 4 4 3]))))) diff --git a/challenge-205/tyler-wardhaugh/clojure/test/c205/t2_test.clj b/challenge-205/tyler-wardhaugh/clojure/test/c205/t2_test.clj new file mode 100644 index 0000000000..af2e00887b --- /dev/null +++ b/challenge-205/tyler-wardhaugh/clojure/test/c205/t2_test.clj @@ -0,0 +1,8 @@ +(ns c205.t2-test + (:require [clojure.test :refer [deftest is testing]] + [c205.t2 :refer [max-xor]])) + +(deftest task-2 + (testing "Task 2 produces the correct results from examples in the description" + (is (= 7 (max-xor [1 2 3 4 5 6 7]))) + (is (= 15 (max-xor [10 5 7 12 8]))))) |
