diff options
9 files changed, 107 insertions, 25 deletions
diff --git a/challenge-139/tyler-wardhaugh/clojure/README.md b/challenge-139/tyler-wardhaugh/clojure/README.md index 7d1b854cdc..17ac43fedd 100644 --- a/challenge-139/tyler-wardhaugh/clojure/README.md +++ b/challenge-139/tyler-wardhaugh/clojure/README.md @@ -1,7 +1,7 @@ -# tw.weekly.c138 +# tw.weekly.c139 -The Weekly Challenge - #138 - Tyler Wardhaugh +The Weekly Challenge - #139 - 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.c138.core + $ clojure -M -m tw.weekly.c139.core # ... or ... $ bb run both @@ -21,15 +21,15 @@ Run the project's tests (which are samples from the task descriptions): Run Task #1 with input - $ clojure -M -m tw.weekly.c138.t1 N + $ clojure -M -m tw.weekly.c139.t1 COLL # ... or ... - $ bb run task-1 N + $ bb run task-1 COLL Run Task #2 with input: - $ clojure -M -m tw.weekly.c138.t2 N + $ clojure -M -m tw.weekly.c139.t2 # ... or ... - $ bb run task-2 N + $ bb run task-2 View available tasks Babashka can run: diff --git a/challenge-139/tyler-wardhaugh/clojure/bb.edn b/challenge-139/tyler-wardhaugh/clojure/bb.edn index be8af5209b..ec79e59292 100644 --- a/challenge-139/tyler-wardhaugh/clojure/bb.edn +++ b/challenge-139/tyler-wardhaugh/clojure/bb.edn @@ -76,7 +76,7 @@ :task (run-task :t1 *command-line-args*)} task-1-bb {:doc "Run Task 1 (via Babashka)" - :task (bb-no-go :t1 *command-line-args*)} + :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-139/tyler-wardhaugh/clojure/deps.edn b/challenge-139/tyler-wardhaugh/clojure/deps.edn index 15aa02e4d4..3386938e59 100644 --- a/challenge-139/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-139/tyler-wardhaugh/clojure/deps.edn @@ -1,8 +1,6 @@ {:paths ["src" "resources"] :deps {org.clojure/clojure {:mvn/version "1.10.3"} - clojure.java-time/clojure.java-time {:mvn/version "0.3.3"} - org.threeten/threeten-extra {:mvn/version "1.7.0"} - org.clojure/math.numeric-tower {:mvn/version "0.0.4"}} + com.hypirion/primes {:mvn/version "0.2.2"}} :aliases {:test {:extra-paths ["test"] :extra-deps {org.clojure/test.check {:mvn/version "1.1.0"} diff --git a/challenge-139/tyler-wardhaugh/clojure/pom.xml b/challenge-139/tyler-wardhaugh/clojure/pom.xml index 8f51ecb4bb..66472e6504 100644 --- a/challenge-139/tyler-wardhaugh/clojure/pom.xml +++ b/challenge-139/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.c138</artifactId> + <artifactId>tw.weekly.c139</artifactId> <version>0.1.0-SNAPSHOT</version> - <name>tw.weekly.c138</name> - <description>Challenge #138</description> - <url>https://github.com/tw.weekly/tw.weekly.c138</url> + <name>tw.weekly.c139</name> + <description>Challenge #139</description> + <url>https://github.com/tw.weekly/tw.weekly.c139</url> <licenses> <license> <name>Eclipse Public License</name> @@ -24,16 +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> - <dependency> - <groupId>org.threeten</groupId> - <artifactId>threeten-extra</artifactId> - <version>1.7.0</version> - </dependency> </dependencies> <build> <sourceDirectory>src</sourceDirectory> diff --git a/challenge-139/tyler-wardhaugh/clojure/src/tw/weekly/c139/core.clj b/challenge-139/tyler-wardhaugh/clojure/src/tw/weekly/c139/core.clj new file mode 100644 index 0000000000..fbf090469f --- /dev/null +++ b/challenge-139/tyler-wardhaugh/clojure/src/tw/weekly/c139/core.clj @@ -0,0 +1,12 @@ +(ns tw.weekly.c139.core + (:require [tw.weekly.c139.t1 :as t1]) + (:require [tw.weekly.c139.t2 :as t2]) + (:gen-class)) + +(defn -main + "Run all tasks" + [& _] + (println "Task #1:") + (t1/-main) + (println "\nTask #2:") + (t2/-main)) diff --git a/challenge-139/tyler-wardhaugh/clojure/src/tw/weekly/c139/t1.clj b/challenge-139/tyler-wardhaugh/clojure/src/tw/weekly/c139/t1.clj new file mode 100644 index 0000000000..532e227a7c --- /dev/null +++ b/challenge-139/tyler-wardhaugh/clojure/src/tw/weekly/c139/t1.clj @@ -0,0 +1,25 @@ +(ns tw.weekly.c139.t1 + (:require [clojure.edn :as edn])) + +;;; +; Task description for TASK #1 › JortSort +;;; +(def DEFAULT-INPUT [[1 2 3 4 5]]) + +(defn ordered? + [coll] + (or (empty? coll) (apply <= coll))) + +(defn shortcircuiting-ordered? + [coll] + (or (empty? coll) + (-> (when-let [[x & xs] coll] + (reduce (fn [p c] (if (<= p c) c (reduced false))) x xs)) + boolean))) + +(defn -main + "Run Task 3 with a given input COLL, defaulting to the first example from the + task description." + [& args] + (let [[COLL] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)] + (println (if (shortcircuiting-ordered? COLL) 1 0)))) diff --git a/challenge-139/tyler-wardhaugh/clojure/src/tw/weekly/c139/t2.clj b/challenge-139/tyler-wardhaugh/clojure/src/tw/weekly/c139/t2.clj new file mode 100644 index 0000000000..737d6ea02b --- /dev/null +++ b/challenge-139/tyler-wardhaugh/clojure/src/tw/weekly/c139/t2.clj @@ -0,0 +1,35 @@ +(ns tw.weekly.c139.t2 + (:require [com.hypirion.primes :as p])) + +;;; +; Task description for TASK #2 › Long Primes +;;; +(def BASE 10) + +(defn cyclic? + [n] + (let [s (str n) + len (count s) + ndigits (set s) + ndigits' (conj ndigits \0) + f (fn [x] (let [odigits (-> x str set)] + (or (= ndigits odigits) (= ndigits' odigits))))] + (every? f (map #(* n %) (range 2 len))))) + +(defn long-prime? + [n] + (-> (biginteger BASE) + (.pow (dec n)) + (.subtract (biginteger 1)) + (.divide (biginteger n)) + cyclic?)) + +(defn long-primes + [] + (let [xf (comp (drop 2) (filter long-prime?) (take 5))] + (sequence xf (p/primes)))) + +(defn -main + "Run Task 2." + [& _] + (long-primes)) diff --git a/challenge-139/tyler-wardhaugh/clojure/test/tw/weekly/c139/t1_test.clj b/challenge-139/tyler-wardhaugh/clojure/test/tw/weekly/c139/t1_test.clj new file mode 100644 index 0000000000..cddc4fa860 --- /dev/null +++ b/challenge-139/tyler-wardhaugh/clojure/test/tw/weekly/c139/t1_test.clj @@ -0,0 +1,15 @@ +(ns tw.weekly.c139.t1-test + (:require [clojure.test :refer [deftest is testing]] + [tw.weekly.c139.t1 :refer [ordered? shortcircuiting-ordered?]])) + +(def both (juxt ordered? shortcircuiting-ordered?)) + +(deftest examples + (testing "Examples from description" + (is (every? true? (both [1 2 3 4 5]))) + (is (every? false? (both [1 3 2 4 5]))))) + +(deftest more + (testing "More interesting tests" + (is (every? true? (both [1]))) + (is (every? true? (both []))))) diff --git a/challenge-139/tyler-wardhaugh/clojure/test/tw/weekly/c139/t2_test.clj b/challenge-139/tyler-wardhaugh/clojure/test/tw/weekly/c139/t2_test.clj new file mode 100644 index 0000000000..b05222cb04 --- /dev/null +++ b/challenge-139/tyler-wardhaugh/clojure/test/tw/weekly/c139/t2_test.clj @@ -0,0 +1,7 @@ +(ns tw.weekly.c139.t2-test + (:require [clojure.test :refer [deftest is testing]] + [tw.weekly.c139.t2 :refer [long-primes]])) + +(deftest examples + (testing "Examples from description" + (is (= [7 17 19 23 29] (long-primes))))) |
