From fb0789c3ddec9b910e380206faf65c4510e2c39d Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Sat, 8 Jan 2022 12:45:20 -0800 Subject: prep for challenge New year, new template! --- challenge-146/tyler-wardhaugh/clojure/.gitignore | 22 +++++++++--- challenge-146/tyler-wardhaugh/clojure/LICENSE | 2 +- challenge-146/tyler-wardhaugh/clojure/README.md | 39 +++++++-------------- challenge-146/tyler-wardhaugh/clojure/bb.edn | 39 ++++++--------------- challenge-146/tyler-wardhaugh/clojure/build.clj | 19 ++++++++++ challenge-146/tyler-wardhaugh/clojure/deps.edn | 21 +++++------ challenge-146/tyler-wardhaugh/clojure/pom.xml | 44 ------------------------ 7 files changed, 71 insertions(+), 115 deletions(-) create mode 100644 challenge-146/tyler-wardhaugh/clojure/build.clj delete mode 100644 challenge-146/tyler-wardhaugh/clojure/pom.xml diff --git a/challenge-146/tyler-wardhaugh/clojure/.gitignore b/challenge-146/tyler-wardhaugh/clojure/.gitignore index 894239c8e8..ac8c2a1062 100644 --- a/challenge-146/tyler-wardhaugh/clojure/.gitignore +++ b/challenge-146/tyler-wardhaugh/clojure/.gitignore @@ -1,16 +1,28 @@ *.class *.jar +*.swp +*~ .*_history -.clj-kondo/ -.hg/ -.hgignore -.lsp/ -.projections.json +.sw* +.vscode +/.calva/output-window/ +/.classpath +/.clj-kondo/ /.cpcache/ +/.eastwood/ +/.factorypath +/.hg/ +/.hgignore +/.java-version /.lein-* /.lsp/ /.nrepl-history /.nrepl-port +/.project +/.projections.json +/.rebel_readline_history +/.settings +/.socket-repl-port /checkouts/ /classes/ /target/ diff --git a/challenge-146/tyler-wardhaugh/clojure/LICENSE b/challenge-146/tyler-wardhaugh/clojure/LICENSE index 7689f30efd..d921d3dffe 100644 --- a/challenge-146/tyler-wardhaugh/clojure/LICENSE +++ b/challenge-146/tyler-wardhaugh/clojure/LICENSE @@ -126,7 +126,7 @@ a commercial product offering. The obligations in this section do not apply to any claims or Losses relating to any actual or alleged intellectual property infringement. In order to qualify, an Indemnified Contributor must: a) promptly notify the Commercial Contributor in writing of such claim, and -b) allow the Commercial Contributor tocontrol, and cooperate with the +b) allow the Commercial Contributor to control, and cooperate with the Commercial Contributor in, the defense and any related settlement negotiations. The Indemnified Contributor may participate in any such claim at its own expense. diff --git a/challenge-146/tyler-wardhaugh/clojure/README.md b/challenge-146/tyler-wardhaugh/clojure/README.md index 213f1884bc..4738867627 100644 --- a/challenge-146/tyler-wardhaugh/clojure/README.md +++ b/challenge-146/tyler-wardhaugh/clojure/README.md @@ -1,48 +1,35 @@ -# tw.weekly.c140 +# c146 - -The Weekly Challenge - #140 - Tyler Wardhaugh +The Weekly Challenge — #146 — Tyler Wardhaugh ## Usage Clojure ([installation instructions](https://clojure.org/guides/getting_started#_clojure_installer_and_cli_tools)) required for `clojure` commands; Babashka ([installation instructions](https://github.com/babashka/babashka#quickstart)) required for the `bb` commands. -Run the project directly (shows default output from both tasks): +Run Task #1: - $ clojure -M -m tw.weekly.c140.core + $ clojure -M:t1 # ... or ... - $ bb run both - -Run the project's tests (which are samples from the task descriptions): + $ bb run task-1 - $ clojure -X:test - # ... or ... - $ bb run test - -Run Task #1 with input +Run Task #2 with input: - $ clojure -M -m tw.weekly.c140.t1 A B + $ clojure -M:t2 M # ... or ... - $ bb run task-1 A B + $ bb run task-2 M -Run Task #2 with input: +Run the project's tests (which are samples from the task descriptions): - $ clojure -M -m tw.weekly.c140.t2 I J K + $ clojure -T:build test # ... or ... - $ bb run task-2 I J K + $ bb run test View available tasks Babashka can run: $ bb tasks -## Project Template - -I used Sean Corfield's clj-new to generate the project template - -See [seancorfield/clj-new: Generate new projects based on clj, Boot, or Leiningen Templates!](https://github.com/seancorfield/clj-new) for more information. - ## License -Copyright © 2021 Tyler Wardhaugh +Copyright © 2022 Tyler Wardhaugh -Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version. +Distributed under the Eclipse Public License version 1.0. diff --git a/challenge-146/tyler-wardhaugh/clojure/bb.edn b/challenge-146/tyler-wardhaugh/clojure/bb.edn index e053ec5de1..e21cd63a1e 100644 --- a/challenge-146/tyler-wardhaugh/clojure/bb.edn +++ b/challenge-146/tyler-wardhaugh/clojure/bb.edn @@ -16,15 +16,13 @@ (defn get-task-ns [task] - (let [glob-target (format "src/tw/weekly/*/%s.clj" (name task)) + (let [glob-target (format "src/*/%s.clj" (name task)) file (->> glob-target (fs/glob ".") first)] (-> file get-first-form second str))) - (defn run-task - ([task args] (run-task task args (get-task-ns task))) - ([task args task-ns] - (let [clj-options (format "-M -m %s " task-ns)] - (apply clojure clj-options args)))) + (defn run-task-clj + [task args] + (apply clojure (str "-M:" task) args)) (defn run-task-bb ([task args] (run-task-bb task args (get-task-ns task))) @@ -43,11 +41,9 @@ (System/exit 1))) clean {:doc "Clean out temporary files" - :task (run! fs/delete-tree - [".nrepl-port" ".cpcache" ".lsp" ".clj-kondo"])} - - generate-pom {:doc "Generate POM file" - :task (clojure "-X:deps mvn-pom")} + :task (do (clojure "-T:build" "clean") + (run! fs/delete-tree + [".nrepl-port" ".cpcache" ".lsp" ".clj-kondo"]))} generate-jar {:doc "Generate JAR file" :depends [generate-pom] @@ -68,35 +64,20 @@ p/check))} test {:doc "Run tests" - :task (clojure "-X:test")} + :task (clojure "-T:build test")} c**** {:doc "CHALLENGE TASKS"} task-1 {:doc "Run Task 1 (via clojure)" - :task (run-task :t1 *command-line-args*)} + :task (run-task-clj :t1 *command-line-args*)} task-1-bb {:doc "Run Task 1 (via Babashka)" :task (run-task-bb :t1 *command-line-args*)} task-2 {:doc "Run Task 2 (via clojure)" - :task (run-task :t2 *command-line-args*)} + :task (run-task-clj :t2 *command-line-args*)} task-2-bb {:doc "Run Task 2 (via Babashka)" :task (run-task-bb :t2 *command-line-args*)} - - both {:doc "Run both tasks (via clojure)" - :task (do - (println "Task 1:") - (run 'task-1) - (println "\nTask 2:") - (run 'task-2))} - - both-bb {:doc "Run both tasks (via Babashka)" - :task (do - (println "Task 1:") - (run 'task-1-bb) - (println "\nTask 2:") - (run 'task-2-bb))} - } } diff --git a/challenge-146/tyler-wardhaugh/clojure/build.clj b/challenge-146/tyler-wardhaugh/clojure/build.clj new file mode 100644 index 0000000000..8a78fbc09a --- /dev/null +++ b/challenge-146/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.c146/c146) +(def version "0.1.0-SNAPSHOT") +(def main 'c146.c146) + +(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-146/tyler-wardhaugh/clojure/deps.edn b/challenge-146/tyler-wardhaugh/clojure/deps.edn index 5b1400b27e..16a95bc2f6 100644 --- a/challenge-146/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-146/tyler-wardhaugh/clojure/deps.edn @@ -1,14 +1,15 @@ {:paths ["src" "resources"] :deps {org.clojure/clojure {:mvn/version "1.10.3"}} :aliases - {:test {:extra-paths ["test"] - :extra-deps {org.clojure/test.check {:mvn/version "1.1.0"} + {:t1 {:main-opts ["-m" "c146.t1"]} + :t2 {:main-opts ["-m" "c146.t2"]} + :build {:deps {io.github.seancorfield/build-clj + {:git/tag "v0.6.3" :git/sha "9b8e09b" + ;; since we're building an app uberjar, we do not + ;; need deps-deploy for clojars.org deployment: + :deps/root "slim"}} + :ns-default build} + :test {:extra-paths ["test"] + :extra-deps {org.clojure/test.check {:mvn/version "1.1.1"} io.github.cognitect-labs/test-runner - {:git/url "https://github.com/cognitect-labs/test-runner" - :sha "62ef1de18e076903374306060ac0e8a752e57c86"}} - :exec-fn cognitect.test-runner.api/test} - :jar {:replace-deps {com.github.seancorfield/depstar {:mvn/version "2.1.278"}} - :exec-fn hf.depstar/uberjar - :exec-args {:aot true - :jar "tw-weekly.jar" - :sync-pom true}}}} + {:git/tag "v0.5.0" :git/sha "48c3c67"}}}}} diff --git a/challenge-146/tyler-wardhaugh/clojure/pom.xml b/challenge-146/tyler-wardhaugh/clojure/pom.xml deleted file mode 100644 index e51de32ab2..0000000000 --- a/challenge-146/tyler-wardhaugh/clojure/pom.xml +++ /dev/null @@ -1,44 +0,0 @@ - - - 4.0.0 - tw.weekly - tw.weekly.c140 - 0.1.0-SNAPSHOT - tw.weekly.c140 - Challenge #140 - https://github.com/tw.weekly/tw.weekly.c140 - - - Eclipse Public License - http://www.eclipse.org/legal/epl-v10.html - - - - - Tyler Wardhaugh - - - - - org.clojure - clojure - 1.10.3 - - - - src - - - - clojars - https://repo.clojars.org/ - - - - - clojars - Clojars repository - https://clojars.org/repo - - - -- cgit From fb357b920a95257266a0063ad44d0480b66fbff2 Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Sat, 8 Jan 2022 12:40:06 -0800 Subject: Ch146 (Clojure): Task 1 --- challenge-146/tyler-wardhaugh/clojure/src/c146/t1.clj | 17 +++++++++++++++++ challenge-146/tyler-wardhaugh/clojure/src/c146/t1b.clj | 11 +++++++++++ .../tyler-wardhaugh/clojure/test/c146/t1_test.clj | 8 ++++++++ 3 files changed, 36 insertions(+) create mode 100644 challenge-146/tyler-wardhaugh/clojure/src/c146/t1.clj create mode 100644 challenge-146/tyler-wardhaugh/clojure/src/c146/t1b.clj create mode 100644 challenge-146/tyler-wardhaugh/clojure/test/c146/t1_test.clj diff --git a/challenge-146/tyler-wardhaugh/clojure/src/c146/t1.clj b/challenge-146/tyler-wardhaugh/clojure/src/c146/t1.clj new file mode 100644 index 0000000000..f7e965c92b --- /dev/null +++ b/challenge-146/tyler-wardhaugh/clojure/src/c146/t1.clj @@ -0,0 +1,17 @@ +(ns c146.t1) + +(def TARGET 10001) + +(defn nth-prime + [n] + (let [p (promise) + sieve (fn [n] + (not-any? #(zero? (rem n %)) + (take-while #(<= (* % %) n) @p)))] + (->> @(deliver p (filter sieve (iterate inc 2))) + (drop (dec n)) + first))) + +(defn -main + [& _] + (println (nth-prime TARGET))) diff --git a/challenge-146/tyler-wardhaugh/clojure/src/c146/t1b.clj b/challenge-146/tyler-wardhaugh/clojure/src/c146/t1b.clj new file mode 100644 index 0000000000..0532597b87 --- /dev/null +++ b/challenge-146/tyler-wardhaugh/clojure/src/c146/t1b.clj @@ -0,0 +1,11 @@ +(ns c146.t1b) + +(def TARGET 10001) + +(defn nth-prime + [n] + (nth (iterate #(.nextProbablePrime %) BigInteger/ONE) n)) + +(defn -main + [& _] + (println (nth-prime TARGET))) diff --git a/challenge-146/tyler-wardhaugh/clojure/test/c146/t1_test.clj b/challenge-146/tyler-wardhaugh/clojure/test/c146/t1_test.clj new file mode 100644 index 0000000000..dcfbb9fb6c --- /dev/null +++ b/challenge-146/tyler-wardhaugh/clojure/test/c146/t1_test.clj @@ -0,0 +1,8 @@ +(ns c146.t1-test + (:require [clojure.test :refer [deftest is testing]] + [c146.t1 :as t1] + [c146.t1b :as t1b])) + +(deftest target + (testing "Target identified in task description" + (is (= 104743 (t1/nth-prime t1/TARGET) (t1b/nth-prime t1b/TARGET))))) -- cgit From b145e66a372043b350cee870df46b81034473b70 Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Sat, 8 Jan 2022 15:52:06 -0800 Subject: Ch146 (Clojure): Task 2 --- .../tyler-wardhaugh/clojure/src/c146/t2.clj | 22 ++++++++++++++++++++++ .../tyler-wardhaugh/clojure/test/c146/t2_test.clj | 11 +++++++++++ 2 files changed, 33 insertions(+) create mode 100644 challenge-146/tyler-wardhaugh/clojure/src/c146/t2.clj create mode 100644 challenge-146/tyler-wardhaugh/clojure/test/c146/t2_test.clj diff --git a/challenge-146/tyler-wardhaugh/clojure/src/c146/t2.clj b/challenge-146/tyler-wardhaugh/clojure/src/c146/t2.clj new file mode 100644 index 0000000000..b259c8edc8 --- /dev/null +++ b/challenge-146/tyler-wardhaugh/clojure/src/c146/t2.clj @@ -0,0 +1,22 @@ +(ns c146.t2 + (:require [clojure.edn :as edn] + [clojure.pprint :refer [cl-format]])) + +; Clojure supports Ratios (including as literals) +(def DEFAULT-INPUT [3/5]) + +(defn parent + [x] + (when (not= x 1) + (let [[n d] ((juxt numerator denominator) x)] + (if (< x 1) + (/ n (- d n)) ; left + (/ (- n d) d))))) ; right + +(defn -main + "Run Task 2 with a given input M, defaulting to the first example from the + task description." + [& args] + (let [[M] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)] + (cl-format true "~{parent = '~a' and grandparent = '~a'~}~%" + (->> M (iterate parent) (drop 1) (take 2))))) diff --git a/challenge-146/tyler-wardhaugh/clojure/test/c146/t2_test.clj b/challenge-146/tyler-wardhaugh/clojure/test/c146/t2_test.clj new file mode 100644 index 0000000000..48dd9ca00a --- /dev/null +++ b/challenge-146/tyler-wardhaugh/clojure/test/c146/t2_test.clj @@ -0,0 +1,11 @@ +(ns c146.t2-test + (:refer-clojure :exclude [ancestors]) + (:require [clojure.test :refer [deftest is testing]] + [c146.t2 :refer [parent]])) + +(def ancestors (juxt parent (comp parent parent))) + +(deftest examples + (testing "Examples from task description" + (is (= [3/2 1/2] (ancestors 3/5))) + (is (= [1/3 1/2] (ancestors 4/3))))) -- cgit From c352307e0bc3f2777d0e2ccd64d5e77e3ce08cf6 Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Sat, 8 Jan 2022 17:02:41 -0800 Subject: flesh out README a bit more --- challenge-146/tyler-wardhaugh/clojure/README.md | 17 ++++++++++++++++- challenge-146/tyler-wardhaugh/clojure/bb.edn | 6 ++++++ challenge-146/tyler-wardhaugh/clojure/deps.edn | 1 + 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/challenge-146/tyler-wardhaugh/clojure/README.md b/challenge-146/tyler-wardhaugh/clojure/README.md index 4738867627..ea5ea9467a 100644 --- a/challenge-146/tyler-wardhaugh/clojure/README.md +++ b/challenge-146/tyler-wardhaugh/clojure/README.md @@ -6,18 +6,33 @@ The Weekly Challenge — #146 — Tyler Wardhaugh Clojure ([installation instructions](https://clojure.org/guides/getting_started#_clojure_installer_and_cli_tools)) required for `clojure` commands; Babashka ([installation instructions](https://github.com/babashka/babashka#quickstart)) required for the `bb` commands. -Run Task #1: +Run Task #1 (implemented with a sieve): $ clojure -M:t1 # ... or ... $ bb run task-1 + # Alternatively, to run it via Babashka: + $ bb run task-1-bb + +Run Task #1b (implemented using JVM's BigInteger library): + + $ clojure -M:t1b + # ... or ... + $ bb run task-1b + + # Alternatively, to run it via Babashka: + $ bb run task-1b-bb + Run Task #2 with input: $ clojure -M:t2 M # ... or ... $ bb run task-2 M + # Alternatively, to run it via Babashka: + $ bb run task-2-bb + Run the project's tests (which are samples from the task descriptions): $ clojure -T:build test diff --git a/challenge-146/tyler-wardhaugh/clojure/bb.edn b/challenge-146/tyler-wardhaugh/clojure/bb.edn index e21cd63a1e..fdbac4a730 100644 --- a/challenge-146/tyler-wardhaugh/clojure/bb.edn +++ b/challenge-146/tyler-wardhaugh/clojure/bb.edn @@ -74,6 +74,12 @@ task-1-bb {:doc "Run Task 1 (via Babashka)" :task (run-task-bb :t1 *command-line-args*)} + task-1b {:doc "Run Task 1 (via clojure)" + :task (run-task-clj :t1b *command-line-args*)} + + task-1b-bb {:doc "Run Task 1 (via Babashka)" + :task (run-task-bb :t1b *command-line-args*)} + task-2 {:doc "Run Task 2 (via clojure)" :task (run-task-clj :t2 *command-line-args*)} diff --git a/challenge-146/tyler-wardhaugh/clojure/deps.edn b/challenge-146/tyler-wardhaugh/clojure/deps.edn index 16a95bc2f6..247ad7b44d 100644 --- a/challenge-146/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-146/tyler-wardhaugh/clojure/deps.edn @@ -2,6 +2,7 @@ :deps {org.clojure/clojure {:mvn/version "1.10.3"}} :aliases {:t1 {:main-opts ["-m" "c146.t1"]} + :t1b {:main-opts ["-m" "c146.t1b"]} :t2 {:main-opts ["-m" "c146.t2"]} :build {:deps {io.github.seancorfield/build-clj {:git/tag "v0.6.3" :git/sha "9b8e09b" -- cgit