diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-01-08 16:59:22 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-08 16:59:22 +0000 |
| commit | d4ebc5e3928a429ab381b9d8271c491dfafea929 (patch) | |
| tree | 34021ad19d1ac9732c09d1b42021a3ab256efe3d | |
| parent | 8820ec6c30077f6255a0268ee8f5b30f42198d79 (diff) | |
| parent | 5e07a8a4d86d7dda74accd37c42ba6740cc1e6d6 (diff) | |
| download | perlweeklychallenge-club-d4ebc5e3928a429ab381b9d8271c491dfafea929.tar.gz perlweeklychallenge-club-d4ebc5e3928a429ab381b9d8271c491dfafea929.tar.bz2 perlweeklychallenge-club-d4ebc5e3928a429ab381b9d8271c491dfafea929.zip | |
Merge pull request #7375 from tylerw/tw/challenge-198
Ch198: Implement Tasks 1 & 2 in Clojure
8 files changed, 82 insertions, 5 deletions
diff --git a/challenge-198/tyler-wardhaugh/clojure/README.md b/challenge-198/tyler-wardhaugh/clojure/README.md index 478f7395fd..c89eb6b103 100644 --- a/challenge-198/tyler-wardhaugh/clojure/README.md +++ b/challenge-198/tyler-wardhaugh/clojure/README.md @@ -1,6 +1,6 @@ -# c197 +# c198 -The Weekly Challenge — #197 — Tyler Wardhaugh +The Weekly Challenge — #198 — Tyler Wardhaugh ## Usage diff --git a/challenge-198/tyler-wardhaugh/clojure/bb.edn b/challenge-198/tyler-wardhaugh/clojure/bb.edn index f02268dce7..5709814635 100644 --- a/challenge-198/tyler-wardhaugh/clojure/bb.edn +++ b/challenge-198/tyler-wardhaugh/clojure/bb.edn @@ -1,6 +1,6 @@ { :paths ["src" "resources"] - :deps {c197/c197 {:local/root "."}} + :deps {c198/c198 {:local/root "."}} :tasks { diff --git a/challenge-198/tyler-wardhaugh/clojure/build.clj b/challenge-198/tyler-wardhaugh/clojure/build.clj new file mode 100644 index 0000000000..b576dba656 --- /dev/null +++ b/challenge-198/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.c198/c198) +(def version "0.1.0-SNAPSHOT") +(def main 'c198.c198) + +(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-198/tyler-wardhaugh/clojure/deps.edn b/challenge-198/tyler-wardhaugh/clojure/deps.edn index a4f57caf90..7ccdfdb16c 100644 --- a/challenge-198/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-198/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" "c197.t1"]} - :t2 {:main-opts ["-m" "c197.t2"]} + {:t1 {:main-opts ["-m" "c198.t1"]} + :t2 {:main-opts ["-m" "c198.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-198/tyler-wardhaugh/clojure/src/c198/t1.clj b/challenge-198/tyler-wardhaugh/clojure/src/c198/t1.clj new file mode 100644 index 0000000000..7e9ca6a80c --- /dev/null +++ b/challenge-198/tyler-wardhaugh/clojure/src/c198/t1.clj @@ -0,0 +1,21 @@ +(ns c198.t1 + (:require + [clojure.edn :as edn])) + +(def DEFAULT-INPUT [[2 5 8 1]]) + +(defn max-gap + [coll] + (if (< (count coll) 2) + 0 + (let [pairs (->> coll sort (partition 2 1)) + grouped (group-by (fn [[a b]] (- b a)) pairs) + max-key (apply max (keys grouped))] + (-> grouped (get max-key []) count)))) + +(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 (max-gap coll)))) diff --git a/challenge-198/tyler-wardhaugh/clojure/src/c198/t2.clj b/challenge-198/tyler-wardhaugh/clojure/src/c198/t2.clj new file mode 100644 index 0000000000..603db7adc9 --- /dev/null +++ b/challenge-198/tyler-wardhaugh/clojure/src/c198/t2.clj @@ -0,0 +1,17 @@ +(ns c198.t2) + +(def DEFAULT-INPUT [10]) +(def CERTAINTY 1000) + +(defn prime-count + [n] + (let [f (fn [x] + (when (.isProbablePrime (BigInteger/valueOf x) CERTAINTY) 1))] + (transduce (keep f) + 0 (range n)))) + +(defn -main + "Run Task 2 with a given input COLL, defaulting to the first example from + the task description." + [& args] + (let [[n] (or (some->> args (map parse-long)) DEFAULT-INPUT)] + (println (prime-count n)))) diff --git a/challenge-198/tyler-wardhaugh/clojure/test/c198/t1_test.clj b/challenge-198/tyler-wardhaugh/clojure/test/c198/t1_test.clj new file mode 100644 index 0000000000..d8c6317530 --- /dev/null +++ b/challenge-198/tyler-wardhaugh/clojure/test/c198/t1_test.clj @@ -0,0 +1,10 @@ +(ns c198.t1-test + (:require [clojure.test :refer [deftest is testing]] + [c198.t1 :refer [max-gap]])) + +(deftest task-1 + (testing "Task 1 produces the correct results from examples in the description" + (is (= 2 (max-gap [2 5 8 1]))) + (is (zero? (max-gap [3])))) + (testing "Task 1 is correct for additional inputs" + (is (zero? (max-gap []))))) diff --git a/challenge-198/tyler-wardhaugh/clojure/test/c198/t2_test.clj b/challenge-198/tyler-wardhaugh/clojure/test/c198/t2_test.clj new file mode 100644 index 0000000000..76409fb485 --- /dev/null +++ b/challenge-198/tyler-wardhaugh/clojure/test/c198/t2_test.clj @@ -0,0 +1,10 @@ +(ns c198.t2-test + (:require [clojure.test :refer [deftest is testing]] + [c198.t2 :refer [prime-count]])) + +(deftest task-2 + (testing "Task 2 produces the correct results from examples in the description" + (is (= 4 (prime-count 10))) + (is (= 6 (prime-count 15))) + (is (= 0 (prime-count 1))) + (is (= 9 (prime-count 25))))) |
