aboutsummaryrefslogtreecommitdiff
path: root/challenge-146
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-01-09 17:14:12 +0000
committerGitHub <noreply@github.com>2022-01-09 17:14:12 +0000
commit543c00b7aedce99655017c3920914302df649995 (patch)
treec3edcf0dc5430622bdeabf142aa4d60a4718608b /challenge-146
parent6c324d646e7831362edb09d1303b1a1d93ed848a (diff)
parentc352307e0bc3f2777d0e2ccd64d5e77e3ce08cf6 (diff)
downloadperlweeklychallenge-club-543c00b7aedce99655017c3920914302df649995.tar.gz
perlweeklychallenge-club-543c00b7aedce99655017c3920914302df649995.tar.bz2
perlweeklychallenge-club-543c00b7aedce99655017c3920914302df649995.zip
Merge pull request #5489 from tylerw/tw/challenge-146
Challenge 146
Diffstat (limited to 'challenge-146')
-rw-r--r--challenge-146/tyler-wardhaugh/clojure/.gitignore22
-rw-r--r--challenge-146/tyler-wardhaugh/clojure/LICENSE2
-rw-r--r--challenge-146/tyler-wardhaugh/clojure/README.md48
-rw-r--r--challenge-146/tyler-wardhaugh/clojure/bb.edn45
-rw-r--r--challenge-146/tyler-wardhaugh/clojure/build.clj19
-rw-r--r--challenge-146/tyler-wardhaugh/clojure/deps.edn22
-rw-r--r--challenge-146/tyler-wardhaugh/clojure/pom.xml44
-rw-r--r--challenge-146/tyler-wardhaugh/clojure/src/c146/t1.clj17
-rw-r--r--challenge-146/tyler-wardhaugh/clojure/src/c146/t1b.clj11
-rw-r--r--challenge-146/tyler-wardhaugh/clojure/src/c146/t2.clj22
-rw-r--r--challenge-146/tyler-wardhaugh/clojure/test/c146/t1_test.clj8
-rw-r--r--challenge-146/tyler-wardhaugh/clojure/test/c146/t2_test.clj11
12 files changed, 159 insertions, 112 deletions
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..ea5ea9467a 100644
--- a/challenge-146/tyler-wardhaugh/clojure/README.md
+++ b/challenge-146/tyler-wardhaugh/clojure/README.md
@@ -1,48 +1,50 @@
-# 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 (implemented with a sieve):
- $ clojure -M -m tw.weekly.c140.core
+ $ clojure -M:t1
# ... or ...
- $ bb run both
+ $ bb run task-1
-Run the project's tests (which are samples from the task descriptions):
+ # Alternatively, to run it via Babashka:
+ $ bb run task-1-bb
- $ clojure -X:test
- # ... or ...
- $ bb run test
+Run Task #1b (implemented using JVM's BigInteger library):
-Run Task #1 with input
-
- $ clojure -M -m tw.weekly.c140.t1 A B
+ $ clojure -M:t1b
# ... or ...
- $ bb run task-1 A B
+ $ bb run task-1b
+
+ # Alternatively, to run it via Babashka:
+ $ bb run task-1b-bb
Run Task #2 with input:
- $ clojure -M -m tw.weekly.c140.t2 I J K
+ $ clojure -M:t2 M
# ... or ...
- $ bb run task-2 I J K
+ $ bb run task-2 M
-View available tasks Babashka can run:
+ # Alternatively, to run it via Babashka:
+ $ bb run task-2-bb
- $ bb tasks
+Run the project's tests (which are samples from the task descriptions):
-## Project Template
+ $ clojure -T:build test
+ # ... or ...
+ $ bb run test
-I used Sean Corfield's clj-new to generate the project template
+View available tasks Babashka can run:
-See [seancorfield/clj-new: Generate new projects based on clj, Boot, or Leiningen Templates!](https://github.com/seancorfield/clj-new) for more information.
+ $ bb tasks
## 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..fdbac4a730 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,26 @@
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-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 :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..247ad7b44d 100644
--- a/challenge-146/tyler-wardhaugh/clojure/deps.edn
+++ b/challenge-146/tyler-wardhaugh/clojure/deps.edn
@@ -1,14 +1,16 @@
{: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"]}
+ :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"
+ ;; 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 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<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.c140</artifactId>
- <version>0.1.0-SNAPSHOT</version>
- <name>tw.weekly.c140</name>
- <description>Challenge #140</description>
- <url>https://github.com/tw.weekly/tw.weekly.c140</url>
- <licenses>
- <license>
- <name>Eclipse Public License</name>
- <url>http://www.eclipse.org/legal/epl-v10.html</url>
- </license>
- </licenses>
- <developers>
- <developer>
- <name>Tyler Wardhaugh</name>
- </developer>
- </developers>
- <dependencies>
- <dependency>
- <groupId>org.clojure</groupId>
- <artifactId>clojure</artifactId>
- <version>1.10.3</version>
- </dependency>
- </dependencies>
- <build>
- <sourceDirectory>src</sourceDirectory>
- </build>
- <repositories>
- <repository>
- <id>clojars</id>
- <url>https://repo.clojars.org/</url>
- </repository>
- </repositories>
- <distributionManagement>
- <repository>
- <id>clojars</id>
- <name>Clojars repository</name>
- <url>https://clojars.org/repo</url>
- </repository>
- </distributionManagement>
-</project>
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/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/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)))))
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)))))