From b74a46400a288a71d511f5ef412ff2dd86a91d95 Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Wed, 6 Oct 2021 07:51:00 -0700 Subject: Ch133 (Clojure): prep for challenge --- challenge-133/tyler-wardhaugh/clojure/.gitignore | 11 +++++------ challenge-133/tyler-wardhaugh/clojure/.projections.json | 10 ++++++++++ challenge-133/tyler-wardhaugh/clojure/README.md | 14 ++++++++------ challenge-133/tyler-wardhaugh/clojure/bb.edn | 4 +--- challenge-133/tyler-wardhaugh/clojure/deps.edn | 3 +-- challenge-133/tyler-wardhaugh/clojure/pom.xml | 13 ++++--------- .../tyler-wardhaugh/clojure/src/tw/weekly/c133/core.clj | 12 ++++++++++++ .../tyler-wardhaugh/clojure/src/tw/weekly/c133/t1.clj | 14 ++++++++++++++ .../tyler-wardhaugh/clojure/src/tw/weekly/c133/t2.clj | 15 +++++++++++++++ 9 files changed, 70 insertions(+), 26 deletions(-) create mode 100644 challenge-133/tyler-wardhaugh/clojure/.projections.json create mode 100644 challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/core.clj create mode 100644 challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/t1.clj create mode 100644 challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/t2.clj diff --git a/challenge-133/tyler-wardhaugh/clojure/.gitignore b/challenge-133/tyler-wardhaugh/clojure/.gitignore index 7f0a72a26b..64867530dc 100644 --- a/challenge-133/tyler-wardhaugh/clojure/.gitignore +++ b/challenge-133/tyler-wardhaugh/clojure/.gitignore @@ -1,16 +1,15 @@ *.class *.jar +.*_history .clj-kondo/ .hg/ .hgignore .lsp/ -.rebel_readline_history -/.cpcache +/.cpcache/ /.lein-* /.lsp/ /.nrepl-history /.nrepl-port -/checkouts -/classes -/dependencies.pdf -/target +/checkouts/ +/classes/ +/target/ diff --git a/challenge-133/tyler-wardhaugh/clojure/.projections.json b/challenge-133/tyler-wardhaugh/clojure/.projections.json new file mode 100644 index 0000000000..fd1070320a --- /dev/null +++ b/challenge-133/tyler-wardhaugh/clojure/.projections.json @@ -0,0 +1,10 @@ +{ + "src/*.clj": { + "alternate": "test/{}_test.clj", + "type": "source" + }, + "test/*_test.clj": { + "alternate": "src/{}.clj", + "type": "test" + } +} \ No newline at end of file diff --git a/challenge-133/tyler-wardhaugh/clojure/README.md b/challenge-133/tyler-wardhaugh/clojure/README.md index 74b8669aa3..7455be8203 100644 --- a/challenge-133/tyler-wardhaugh/clojure/README.md +++ b/challenge-133/tyler-wardhaugh/clojure/README.md @@ -1,7 +1,7 @@ -# tw.weekly.c132 +# tw.weekly.c133 -The Weekly Challenge - #132 - Tyler Wardhaugh +The Weekly Challenge - #133 - Tyler Wardhaugh ## Usage @@ -9,7 +9,7 @@ Clojure ([installation instructions](https://clojure.org/guides/getting_started# Run the project directly (shows default output from both tasks): - $ clojure -M -m tw.weekly.c132.core + $ clojure -M -m tw.weekly.c133.core # ... or ... $ bb run both @@ -21,13 +21,15 @@ Run the project's tests (which are samples from the task descriptions): Run Task #1 with input - $ clojure -M -m tw.weekly.c132.t1 D + $ clojure -M -m tw.weekly.c133.t1 N + # ... or ... + $ bb run task-1 N Run Task #2 with input: - $ clojure -M -m tw.weekly.c132.t2 H1 H2 I1 I2 + $ clojure -M -m tw.weekly.c133.t2 N # ... or ... - $ bb run task-2 H1 H2 I1 I2 + $ bb run task-2 N View available tasks Babashka can run: diff --git a/challenge-133/tyler-wardhaugh/clojure/bb.edn b/challenge-133/tyler-wardhaugh/clojure/bb.edn index a0e8b96a48..4cd11817b8 100644 --- a/challenge-133/tyler-wardhaugh/clojure/bb.edn +++ b/challenge-133/tyler-wardhaugh/clojure/bb.edn @@ -63,9 +63,7 @@ :task (run-task :t1 *command-line-args*)} task-1-bb {:doc "Run Task 1 (via Babashka)" - :task (binding [*out* *err*] - (println "error: can't run Task 1 via Babashka because it depends on some incompatible libraries.") - (System/exit 1))} + :task (run-task-bb :t1 *command-line-args*)} task-2 {:doc "Run Task 2 (via clojure)" :task (run-task :t2 *command-line-args*)} diff --git a/challenge-133/tyler-wardhaugh/clojure/deps.edn b/challenge-133/tyler-wardhaugh/clojure/deps.edn index e821835450..5b1400b27e 100644 --- a/challenge-133/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-133/tyler-wardhaugh/clojure/deps.edn @@ -1,6 +1,5 @@ {:paths ["src" "resources"] - :deps {org.clojure/clojure {:mvn/version "1.10.3"} - clojure.java-time/clojure.java-time {:mvn/version "0.3.3"}} + :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"} diff --git a/challenge-133/tyler-wardhaugh/clojure/pom.xml b/challenge-133/tyler-wardhaugh/clojure/pom.xml index ca7a3f1f2a..fce28c3b21 100644 --- a/challenge-133/tyler-wardhaugh/clojure/pom.xml +++ b/challenge-133/tyler-wardhaugh/clojure/pom.xml @@ -2,11 +2,11 @@ 4.0.0 tw.weekly - tw.weekly.c132 + tw.weekly.c133 0.1.0-SNAPSHOT - tw.weekly.c132 - Challenge #132 - https://github.com/tw.weekly/tw.weekly.c132 + tw.weekly.c133 + Challenge #133 + https://github.com/tw.weekly/tw.weekly.c133 Eclipse Public License @@ -24,11 +24,6 @@ clojure 1.10.3 - - clojure.java-time - clojure.java-time - 0.3.3 - src diff --git a/challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/core.clj b/challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/core.clj new file mode 100644 index 0000000000..d926970dc3 --- /dev/null +++ b/challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/core.clj @@ -0,0 +1,12 @@ +(ns tw.weekly.c133.core + (:require [tw.weekly.c133.t1 :as t1]) + (:require [tw.weekly.c133.t2 :as t2]) + (:gen-class)) + +(defn -main + "Run all tasks" + [& _] + (println "Task #1:") + (t1/-main) + (println "\nTask #2:") + (t2/-main)) diff --git a/challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/t1.clj b/challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/t1.clj new file mode 100644 index 0000000000..64c77e4ed7 --- /dev/null +++ b/challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/t1.clj @@ -0,0 +1,14 @@ +(ns tw.weekly.c133.t1 + (:require [clojure.edn :as edn])) + +;;; +; Task description for TASK #1 › Integer Square Root +;;; +(def DEFAULT-INPUT [10]) + +(defn -main + "Run Task 1 with a given input N, defaulting to the first example from the + task description." + [& args] + (let [[N] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)] + )) diff --git a/challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/t2.clj b/challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/t2.clj new file mode 100644 index 0000000000..ad200456c8 --- /dev/null +++ b/challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/t2.clj @@ -0,0 +1,15 @@ +(ns tw.weekly.c132.t2 + (:require [clojure.edn :as edn] + [clojure.pprint :refer [cl-format]])) + +;;; +; Task description for TASK #2, Smith Numbers +;;; +(def DEFAULT-INPUT [10]) + +(defn -main + "Run Task 2 with a given input N, defaulting to the first + example from the task description." + [& args] + (let [[N] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)] + )) -- cgit From c138ad6152c0ee9f2a456b586e5ea73605566c46 Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Wed, 6 Oct 2021 07:51:00 -0700 Subject: Ch133 (Clojure): Task 1 --- .../tyler-wardhaugh/clojure/src/tw/weekly/c133/t1.clj | 19 ++++++++++++++++++- .../clojure/test/tw/weekly/c133/t1_test.clj | 11 +++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 challenge-133/tyler-wardhaugh/clojure/test/tw/weekly/c133/t1_test.clj diff --git a/challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/t1.clj b/challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/t1.clj index 64c77e4ed7..52c713ec7e 100644 --- a/challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/t1.clj +++ b/challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/t1.clj @@ -6,9 +6,26 @@ ;;; (def DEFAULT-INPUT [10]) +(defn isqrt-easy + "Return the integer square root of n, the easy way." + [n] + (int (Math/sqrt n))) + +(defn isqrt + "Return the integer square root of n, a slightly more manual way." + [n] + (let [x (quot n 2) + next-x (fn [x] (quot (+ x (quot n x)) 2)) + xf (comp + (partition-all 2) + (drop-while (partial apply not=)))] + (->> (iterate next-x x) + (sequence xf) + ffirst))) + (defn -main "Run Task 1 with a given input N, defaulting to the first example from the task description." [& args] (let [[N] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)] - )) + (println (isqrt N)))) diff --git a/challenge-133/tyler-wardhaugh/clojure/test/tw/weekly/c133/t1_test.clj b/challenge-133/tyler-wardhaugh/clojure/test/tw/weekly/c133/t1_test.clj new file mode 100644 index 0000000000..126ea39d18 --- /dev/null +++ b/challenge-133/tyler-wardhaugh/clojure/test/tw/weekly/c133/t1_test.clj @@ -0,0 +1,11 @@ +(ns tw.weekly.c133.t1-test + (:require [clojure.test :refer [deftest is testing]] + [tw.weekly.c133.t1 :refer [isqrt isqrt-easy]])) + +(deftest examples + (testing "Examples from description" + (let [both (juxt isqrt isqrt-easy)] + (is (apply = 3 (both 10))) + (is (apply = 5 (both 27))) + (is (apply = 9 (both 85))) + (is (apply = 10 (both 101)))))) -- cgit From ae07cd04f89371e88ca57ca997be8016d74959bd Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Wed, 6 Oct 2021 07:51:00 -0700 Subject: Ch133 (Clojure): Task 2 --- .../clojure/src/tw/weekly/c133/t2.clj | 32 +++++++++++++++++++--- .../clojure/test/tw/weekly/c133/t2_test.clj | 15 ++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 challenge-133/tyler-wardhaugh/clojure/test/tw/weekly/c133/t2_test.clj diff --git a/challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/t2.clj b/challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/t2.clj index ad200456c8..ed11beff5a 100644 --- a/challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/t2.clj +++ b/challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/t2.clj @@ -1,15 +1,39 @@ -(ns tw.weekly.c132.t2 - (:require [clojure.edn :as edn] - [clojure.pprint :refer [cl-format]])) +(ns tw.weekly.c133.t2 + (:require [clojure.edn :as edn])) ;;; ; Task description for TASK #2, Smith Numbers ;;; (def DEFAULT-INPUT [10]) +; based on source: https://rosettacode.org/wiki/Prime_decomposition#Clojure +(defn prime-factors + "Return a list of factors of n." + ([n] + (prime-factors n 2 (list))) + ([n k acc] + (cond + (= 1 n) acc + (zero? (rem n k)) (recur (quot n k) k (cons k acc)) + :else (recur n (inc k) acc)))) + +(defn sum-digits + "Sum the individual digits of n." + [n] + (transduce (map #(Character/getNumericValue %)) + (str n))) + +(defn smith? + "Returns true if n is a Smith Number." + [n] + (let [pfs (prime-factors n)] + (and (< 1 (count pfs)) + (= (sum-digits n) (transduce (map sum-digits) + pfs))))) + +(def smiths (->> (iterate inc 1) (filter smith?))) + (defn -main "Run Task 2 with a given input N, defaulting to the first example from the task description." [& args] (let [[N] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)] - )) + (run! println (take N smiths)))) diff --git a/challenge-133/tyler-wardhaugh/clojure/test/tw/weekly/c133/t2_test.clj b/challenge-133/tyler-wardhaugh/clojure/test/tw/weekly/c133/t2_test.clj new file mode 100644 index 0000000000..424873cbea --- /dev/null +++ b/challenge-133/tyler-wardhaugh/clojure/test/tw/weekly/c133/t2_test.clj @@ -0,0 +1,15 @@ +(ns tw.weekly.c133.t2-test + (:require [clojure.test :refer [deftest is testing]] + [tw.weekly.c133.t2 :refer [smiths]])) + +; source: https://oeis.org/A006753/list +(def oeis-smith-numbers + [4,22,27,58,85,94,121,166,202,265,274,319,346,355, + 378,382,391,438,454,483,517,526,535,562,576,588, + 627,634,636,645,648,654,663,666,690,706,728,729, + 762,778,825,852,861,895,913,915,922,958,985,1086, + 1111,1165,1219]) + +(deftest first-10 + (testing "Verify the first 10 Smith Numbers" + (is (= (take 10 oeis-smith-numbers) (take 10 smiths))))) -- cgit