aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-10-06 18:55:43 +0100
committerGitHub <noreply@github.com>2021-10-06 18:55:43 +0100
commit5020fabf9cc53ace127f88dd55d1baebdc5e5ea1 (patch)
treedf5d2a4c81f31f35690e22a0a0bd6cc05cafbf68
parentb4fcbf6084bfb2957bf909be0f0597437f7a29ef (diff)
parentae07cd04f89371e88ca57ca997be8016d74959bd (diff)
downloadperlweeklychallenge-club-5020fabf9cc53ace127f88dd55d1baebdc5e5ea1.tar.gz
perlweeklychallenge-club-5020fabf9cc53ace127f88dd55d1baebdc5e5ea1.tar.bz2
perlweeklychallenge-club-5020fabf9cc53ace127f88dd55d1baebdc5e5ea1.zip
Merge pull request #4979 from tylerw/tw/challenge-133
Challenge 133
-rw-r--r--challenge-133/tyler-wardhaugh/clojure/.gitignore11
-rw-r--r--challenge-133/tyler-wardhaugh/clojure/.projections.json10
-rw-r--r--challenge-133/tyler-wardhaugh/clojure/README.md14
-rw-r--r--challenge-133/tyler-wardhaugh/clojure/bb.edn4
-rw-r--r--challenge-133/tyler-wardhaugh/clojure/deps.edn3
-rw-r--r--challenge-133/tyler-wardhaugh/clojure/pom.xml13
-rw-r--r--challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/core.clj12
-rw-r--r--challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/t1.clj31
-rw-r--r--challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/t2.clj39
-rw-r--r--challenge-133/tyler-wardhaugh/clojure/test/tw/weekly/c133/t1_test.clj11
-rw-r--r--challenge-133/tyler-wardhaugh/clojure/test/tw/weekly/c133/t2_test.clj15
11 files changed, 137 insertions, 26 deletions
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 @@
<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.c132</artifactId>
+ <artifactId>tw.weekly.c133</artifactId>
<version>0.1.0-SNAPSHOT</version>
- <name>tw.weekly.c132</name>
- <description>Challenge #132</description>
- <url>https://github.com/tw.weekly/tw.weekly.c132</url>
+ <name>tw.weekly.c133</name>
+ <description>Challenge #133</description>
+ <url>https://github.com/tw.weekly/tw.weekly.c133</url>
<licenses>
<license>
<name>Eclipse Public License</name>
@@ -24,11 +24,6 @@
<artifactId>clojure</artifactId>
<version>1.10.3</version>
</dependency>
- <dependency>
- <groupId>clojure.java-time</groupId>
- <artifactId>clojure.java-time</artifactId>
- <version>0.3.3</version>
- </dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
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..52c713ec7e
--- /dev/null
+++ b/challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/t1.clj
@@ -0,0 +1,31 @@
+(ns tw.weekly.c133.t1
+ (:require [clojure.edn :as edn]))
+
+;;;
+; Task description for TASK #1 › Integer Square Root
+;;;
+(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/src/tw/weekly/c133/t2.clj b/challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/t2.clj
new file mode 100644
index 0000000000..ed11beff5a
--- /dev/null
+++ b/challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/t2.clj
@@ -0,0 +1,39 @@
+(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/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))))))
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)))))