diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-10-29 08:04:06 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-29 08:04:06 +0100 |
| commit | 8930161a531fb6704be2c3f9ce605daf2480feba (patch) | |
| tree | 5088c7bf653a921d41973a6762b5b99b25b8d735 | |
| parent | dbdcfc75c8230b8f1b91440db90a0bbd27e7dd43 (diff) | |
| parent | 950090fb8ed1665038477f469306b468dbb7dfb8 (diff) | |
| download | perlweeklychallenge-club-8930161a531fb6704be2c3f9ce605daf2480feba.tar.gz perlweeklychallenge-club-8930161a531fb6704be2c3f9ce605daf2480feba.tar.bz2 perlweeklychallenge-club-8930161a531fb6704be2c3f9ce605daf2480feba.zip | |
Merge pull request #5117 from tylerw/tw/challenge-136
Challenge 136
10 files changed, 94 insertions, 12 deletions
diff --git a/challenge-136/tyler-wardhaugh/clojure/README.md b/challenge-136/tyler-wardhaugh/clojure/README.md index 203cf79b7b..d05825bedb 100644 --- a/challenge-136/tyler-wardhaugh/clojure/README.md +++ b/challenge-136/tyler-wardhaugh/clojure/README.md @@ -1,7 +1,7 @@ -# tw.weekly.c135 +# tw.weekly.c136 -The Weekly Challenge - #135 - Tyler Wardhaugh +The Weekly Challenge - #136 - 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.c135.core + $ clojure -M -m tw.weekly.c136.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.c135.t1 N + $ clojure -M -m tw.weekly.c136.t1 M N + # ... or ... + $ bb run task-1 M N Run Task #2 with input: - $ clojure -M -m tw.weekly.c135.t2 SEDOL + $ clojure -M -m tw.weekly.c136.t2 N # ... or ... - $ bb run task-2 SEDOL + $ bb run task-2 N View available tasks Babashka can run: diff --git a/challenge-136/tyler-wardhaugh/clojure/bb.edn b/challenge-136/tyler-wardhaugh/clojure/bb.edn index 69331331eb..2299ced5c9 100644 --- a/challenge-136/tyler-wardhaugh/clojure/bb.edn +++ b/challenge-136/tyler-wardhaugh/clojure/bb.edn @@ -70,7 +70,9 @@ :task (run-task :t2 *command-line-args*)} task-2-bb {:doc "Run Task 2 (via Babashka)" - :task (run-task-bb :t2 *command-line-args*)} + :task (binding [*out* *err*] + (println "error: can't run Task 2 via Babashka because it depends on some incompatible libraries.") + (System/exit 1))} both {:doc "Run both tasks (via clojure)" :task (do diff --git a/challenge-136/tyler-wardhaugh/clojure/deps.edn b/challenge-136/tyler-wardhaugh/clojure/deps.edn index 5b1400b27e..99b46a9e15 100644 --- a/challenge-136/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-136/tyler-wardhaugh/clojure/deps.edn @@ -1,5 +1,6 @@ {:paths ["src" "resources"] - :deps {org.clojure/clojure {:mvn/version "1.10.3"}} + :deps {org.clojure/clojure {:mvn/version "1.10.3"} + org.clojure/math.combinatorics {:mvn/version "0.1.6"}} :aliases {:test {:extra-paths ["test"] :extra-deps {org.clojure/test.check {:mvn/version "1.1.0"} diff --git a/challenge-136/tyler-wardhaugh/clojure/pom.xml b/challenge-136/tyler-wardhaugh/clojure/pom.xml index 4fb7c617a2..fbbe52d202 100644 --- a/challenge-136/tyler-wardhaugh/clojure/pom.xml +++ b/challenge-136/tyler-wardhaugh/clojure/pom.xml @@ -2,11 +2,11 @@ <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>tw.weekly</groupId> - <artifactId>tw.weekly.c135</artifactId> + <artifactId>tw.weekly.c136</artifactId> <version>0.1.0-SNAPSHOT</version> - <name>tw.weekly.c135</name> - <description>Challenge #135</description> - <url>https://github.com/tw.weekly/tw.weekly.c135</url> + <name>tw.weekly.c136</name> + <description>Challenge #136</description> + <url>https://github.com/tw.weekly/tw.weekly.c136</url> <licenses> <license> <name>Eclipse Public License</name> diff --git a/challenge-136/tyler-wardhaugh/clojure/src/tw/weekly/c136/core.clj b/challenge-136/tyler-wardhaugh/clojure/src/tw/weekly/c136/core.clj new file mode 100644 index 0000000000..4f1dcd8562 --- /dev/null +++ b/challenge-136/tyler-wardhaugh/clojure/src/tw/weekly/c136/core.clj @@ -0,0 +1,12 @@ +(ns tw.weekly.c136.core + (:require [tw.weekly.c136.t1 :as t1]) + (:require [tw.weekly.c136.t2 :as t2]) + (:gen-class)) + +(defn -main + "Run all tasks" + [& _] + (println "Task #1:") + (t1/-main) + (println "\nTask #2:") + (t2/-main)) diff --git a/challenge-136/tyler-wardhaugh/clojure/src/tw/weekly/c136/t1.clj b/challenge-136/tyler-wardhaugh/clojure/src/tw/weekly/c136/t1.clj new file mode 100644 index 0000000000..dddde5d12f --- /dev/null +++ b/challenge-136/tyler-wardhaugh/clojure/src/tw/weekly/c136/t1.clj @@ -0,0 +1,26 @@ +(ns tw.weekly.c136.t1 + (:require [clojure.edn :as edn])) + +;;; +; Task description for TASK #1 › Two Friendly +;;; +(def DEFAULT-INPUT [8 24]) + +(defn- power-of-2? + "Assuming n is a positive BigInteger, is it a power of 2?" + [n] + (= (.getLowestSetBit n) (dec (.bitLength n)))) + +(defn two-friendly? + "Determine if two positive integers m and n are two friendly, that is: + gcd(m, n) = 2^p where p > 0." + [m n] + (when-let [gcd (.gcd (biginteger m) (biginteger n))] + (and (< 1 gcd) (power-of-2? gcd)))) + +(defn -main + "Run Task 1 with a given input M and N, defaulting to the first example from + the task description." + [& args] + (let [[M N] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)] + (println (if (two-friendly? M N) 1 0)))) diff --git a/challenge-136/tyler-wardhaugh/clojure/src/tw/weekly/c136/t2.clj b/challenge-136/tyler-wardhaugh/clojure/src/tw/weekly/c136/t2.clj new file mode 100644 index 0000000000..b9de2a6b8a --- /dev/null +++ b/challenge-136/tyler-wardhaugh/clojure/src/tw/weekly/c136/t2.clj @@ -0,0 +1,20 @@ +(ns tw.weekly.c136.t2 + (:require [clojure.edn :as edn] + [tw.weekly.ch-1 :as c77-t1])) + +;;; +; Task description for TASK #2 › Fibonacci Sequence +;;; +(def DEFAULT-INPUT [16]) + +; Reuse our solution from Challenge 77, Task #2 (Fibonacci Sum) +(defn fibo-sum-count + [n] + (count (c77-t1/fibo-sum n))) + +(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)] + (println (fibo-sum-count N)))) diff --git a/challenge-136/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj b/challenge-136/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj new file mode 120000 index 0000000000..a4157827b9 --- /dev/null +++ b/challenge-136/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj @@ -0,0 +1 @@ +../../../../../../challenge-077/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj
\ No newline at end of file diff --git a/challenge-136/tyler-wardhaugh/clojure/test/tw/weekly/c136/t1_test.clj b/challenge-136/tyler-wardhaugh/clojure/test/tw/weekly/c136/t1_test.clj new file mode 100644 index 0000000000..cf5b6ea6e0 --- /dev/null +++ b/challenge-136/tyler-wardhaugh/clojure/test/tw/weekly/c136/t1_test.clj @@ -0,0 +1,9 @@ +(ns tw.weekly.c136.t1-test + (:require [clojure.test :refer [deftest is testing]] + [tw.weekly.c136.t1 :refer [two-friendly?]])) + +(deftest examples + (testing "Examples from description" + (is (true? (two-friendly? 8 24))) + (is (false? (two-friendly? 26 39))) + (is (true? (two-friendly? 4 10))))) diff --git a/challenge-136/tyler-wardhaugh/clojure/test/tw/weekly/c136/t2_test.clj b/challenge-136/tyler-wardhaugh/clojure/test/tw/weekly/c136/t2_test.clj new file mode 100644 index 0000000000..79deae4cc8 --- /dev/null +++ b/challenge-136/tyler-wardhaugh/clojure/test/tw/weekly/c136/t2_test.clj @@ -0,0 +1,9 @@ +(ns tw.weekly.c136.t2-test + (:require [clojure.test :refer [deftest is testing]] + [tw.weekly.c136.t2 :refer [fibo-sum-count]])) + +(deftest examples + (testing "Examples from description" + (is (= 4 (fibo-sum-count 16))) + (is (= 2 (fibo-sum-count 9))) + (is (= 2 (fibo-sum-count 15))))) |
