diff options
12 files changed, 136 insertions, 16 deletions
diff --git a/challenge-088/tyler-wardhaugh/clojure/README.md b/challenge-088/tyler-wardhaugh/clojure/README.md index 7a610a7578..49181152f7 100644 --- a/challenge-088/tyler-wardhaugh/clojure/README.md +++ b/challenge-088/tyler-wardhaugh/clojure/README.md @@ -1,13 +1,13 @@ -# tw.weekly.c87 +# tw.weekly.c88 -The Weekly Challenge - #087 - Tyler Wardhaugh +The Weekly Challenge - #088 - Tyler Wardhaugh ## Usage Run the project directly (shows default output from both tasks): - $ clojure -M -m tw.weekly.c87.core + $ clojure -M -m tw.weekly.c88.core Run the project's tests (which are samples from the task descriptions): @@ -15,11 +15,11 @@ Run the project's tests (which are samples from the task descriptions): Run Task #1 with input - $ clojure -M -m tw.weekly.c87.t1 N1 N2 N3... + $ clojure -M -m tw.weekly.c88.t1 N1 N2 N3... Run Task #2 with input: - $ clojure -M -m tw.weekly.c87.t2 MATRIX-FILE + $ clojure -M -m tw.weekly.c88.t2 MATRIX-FILE ## Project Template diff --git a/challenge-088/tyler-wardhaugh/clojure/deps.edn b/challenge-088/tyler-wardhaugh/clojure/deps.edn index 096b24ac34..f09671a417 100644 --- a/challenge-088/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-088/tyler-wardhaugh/clojure/deps.edn @@ -1,6 +1,7 @@ {:paths ["src" "resources"] :deps {org.clojure/clojure {:mvn/version "1.10.1"} - net.mikera/core.matrix {:mvn/version "0.62.0"}} + net.mikera/core.matrix {:mvn/version "0.62.0"} + org.clojure/math.numeric-tower {:mvn/version "0.0.4"}} :aliases {:test {:extra-paths ["test"] :extra-deps {org.clojure/test.check {:mvn/version "1.0.0"}}} @@ -11,5 +12,5 @@ :main-opts ["-m" "cognitect.test-runner" "-d" "test"]} :uberjar {:extra-deps {seancorfield/depstar {:mvn/version "1.0.94"}} - :main-opts ["-m" "hf.depstar.uberjar" "tw.weekly.c87.jar" - "-C" "-m" "tw.weekly.c87"]}}} + :main-opts ["-m" "hf.depstar.uberjar" "tw.weekly.c88.jar" + "-C" "-m" "tw.weekly.c88"]}}} diff --git a/challenge-088/tyler-wardhaugh/clojure/pom.xml b/challenge-088/tyler-wardhaugh/clojure/pom.xml index 2eeb46cb8c..6038b97734 100644 --- a/challenge-088/tyler-wardhaugh/clojure/pom.xml +++ b/challenge-088/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.c87</artifactId> + <artifactId>tw.weekly.c88</artifactId> <version>0.1.0-SNAPSHOT</version> - <name>tw.weekly.c87</name> - <description>Challenge #087</description> - <url>https://github.com/tw.weekly/tw.weekly.c87</url> + <name>tw.weekly.c88</name> + <description>Challenge #088</description> + <url>https://github.com/tw.weekly/tw.weekly.c88</url> <licenses> <license> <name>Eclipse Public License</name> @@ -19,9 +19,9 @@ </developer> </developers> <scm> - <url>https://github.com/tw.weekly/tw.weekly.c87</url> - <connection>scm:git:git://github.com/tw.weekly/tw.weekly.c87.git</connection> - <developerConnection>scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c87.git</developerConnection> + <url>https://github.com/tw.weekly/tw.weekly.c88</url> + <connection>scm:git:git://github.com/tw.weekly/tw.weekly.c88.git</connection> + <developerConnection>scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c88.git</developerConnection> <tag>HEAD</tag> </scm> <dependencies> diff --git a/challenge-088/tyler-wardhaugh/clojure/resources/matrix-1 b/challenge-088/tyler-wardhaugh/clojure/resources/matrix-1 new file mode 100644 index 0000000000..32009c9873 --- /dev/null +++ b/challenge-088/tyler-wardhaugh/clojure/resources/matrix-1 @@ -0,0 +1,3 @@ +[ 1, 2, 3 ] +[ 4, 5, 6 ] +[ 7, 8, 9 ] diff --git a/challenge-088/tyler-wardhaugh/clojure/resources/matrix-2 b/challenge-088/tyler-wardhaugh/clojure/resources/matrix-2 new file mode 100644 index 0000000000..ebee37d5d1 --- /dev/null +++ b/challenge-088/tyler-wardhaugh/clojure/resources/matrix-2 @@ -0,0 +1,4 @@ +[ 1, 2, 3, 4 ] +[ 5, 6, 7, 8 ] +[ 9, 10, 11, 12 ] +[ 13, 14, 15, 16 ] diff --git a/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/core.clj b/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/core.clj new file mode 100644 index 0000000000..171b6fcb2e --- /dev/null +++ b/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/core.clj @@ -0,0 +1,12 @@ +(ns tw.weekly.c88.core + (:require [tw.weekly.c88.t1 :as t1]) + (:require [tw.weekly.c88.t2 :as t2]) + (:gen-class)) + +(defn -main + "Run all tasks" + [& _] + (println "Task #1:") + (t1/-main) + (println "\nTask #2:") + (t2/-main)) diff --git a/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/t1.clj b/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/t1.clj new file mode 100644 index 0000000000..354eef276d --- /dev/null +++ b/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/t1.clj @@ -0,0 +1,24 @@ +(ns tw.weekly.c88.t1 + (:require [clojure.edn :as edn]) + (:require [clojure.math.numeric-tower :as math]) + (:require [clojure.pprint :refer [cl-format]])) + +;;; +; Task description for TASK #1 › Array of Product +;;; + +(defn array-of-product + "Return an array @M where $M[i] is the product of all elements of @N except the index $N[i]." + [coll] + (let [freqs (frequencies coll) + product (transduce (map (partial apply math/expt)) * freqs) + cache-xf (map (juxt key (comp (partial / product) key))) + cache (into {} cache-xf freqs)] + (sequence (map cache) coll))) + +(defn -main + "Run Task 1 with a list of numbers N, defaulting to the + first example given in the task description." + [& args] + (let [N (or (some->> args (map edn/read-string)) [5 2 1 4 3])] + (cl-format true "@M = (~{~a~^, ~})" (array-of-product N)))) diff --git a/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/t2.clj b/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/t2.clj new file mode 100644 index 0000000000..5ebb1933c0 --- /dev/null +++ b/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/t2.clj @@ -0,0 +1,35 @@ +(ns tw.weekly.c88.t2 + (:require [tw.weekly.c88.util :as util]) + (:require [clojure.java.io :as io]) + (:require [clojure.pprint :refer [cl-format]]) + (:require [clojure.core.matrix :as m])) + +;;; +; Task description for TASK #2 › Spiral Matrix +;;; + +(defn make-matrix + "Instantiate a matrix from a matrix file" + [matrix-file] + (-> matrix-file util/parse-matrix-file m/matrix)) + +(defn rotate-matrix + "Rotate a matrix counterclockwise" + [matrix] + (-> matrix m/transpose reverse)) + +(defn spiral-matrix + "Return a sequence of matrix items in spiral order." + [matrix] + (when (m/eseq matrix) + (let [row (m/get-row matrix 0) + remaining-rows (-> matrix (m/select :rest :all) m/rows)] + (concat row (-> remaining-rows rotate-matrix spiral-matrix))))) + +(defn -main + "Run Task 2 with a given file containing a matrix, defaulting + to the example given in the task description." + [& args] + (let [matrix-file (or (some-> args first io/file) (io/resource "matrix-1")) + mseq (-> matrix-file make-matrix spiral-matrix)] + (cl-format true "[ ~{~a~^, ~} ]~%" mseq))) diff --git a/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/util.clj b/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/util.clj new file mode 100644 index 0000000000..453e2ff33c --- /dev/null +++ b/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/util.clj @@ -0,0 +1,11 @@ +(ns tw.weekly.c88.util + (:require [clojure.java.io :as io]) + (:require [clojure.edn :as edn])) + +(defn parse-matrix-file + "Parse a matrix file and return a matrix" + [matrix-file] + (with-open [in (io/reader matrix-file)] + (-> (slurp in) + (as-> x (str \[ x \])) + edn/read-string))) diff --git a/challenge-088/tyler-wardhaugh/clojure/test/tw/weekly/c88_test.clj b/challenge-088/tyler-wardhaugh/clojure/test/tw/weekly/c88_test.clj new file mode 100644 index 0000000000..cd82616ee4 --- /dev/null +++ b/challenge-088/tyler-wardhaugh/clojure/test/tw/weekly/c88_test.clj @@ -0,0 +1,21 @@ +(ns tw.weekly.c88-test + (:require [clojure.test :refer [deftest is testing]] + [clojure.java.io :as io] + [tw.weekly.c88.t1 :refer [array-of-product]] + [tw.weekly.c88.t2 :refer [make-matrix spiral-matrix]])) + +(deftest task-1 + (testing "Task 1 Array of Product" + (is (= [24 60 120 30 40] (array-of-product [5 2 1 4 3]))) + (is (= [12 24 6 8] (array-of-product [2 1 4 3]))))) + +(def task-2-helper + ^{:doc "Helper function to test Task 2"} + (comp spiral-matrix make-matrix io/resource)) + +(deftest task-2 + (testing "Task 2 Spiral Matrix" + (is (= [1 2 3 6 9 8 7 4 5] + (task-2-helper "matrix-1"))) + (is (= [1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10] + (task-2-helper "matrix-2"))))) diff --git a/challenge-088/tyler-wardhaugh/lua/README.md b/challenge-088/tyler-wardhaugh/lua/README.md index 62141395f2..9473c1a0a4 100644 --- a/challenge-088/tyler-wardhaugh/lua/README.md +++ b/challenge-088/tyler-wardhaugh/lua/README.md @@ -1,7 +1,7 @@ # The Weekly Challenge -The Weekly Challenge - #087 - Tyler Wardhaugh +The Weekly Challenge - #088 - Tyler Wardhaugh ## Usage diff --git a/challenge-088/tyler-wardhaugh/lua/run.lua b/challenge-088/tyler-wardhaugh/lua/run.lua new file mode 100755 index 0000000000..c6e7473bee --- /dev/null +++ b/challenge-088/tyler-wardhaugh/lua/run.lua @@ -0,0 +1,9 @@ +#!/usr/bin/env lua + +local filename = arg[1] +local run_args = table.move(arg, 2, #arg, 1, {}) + +io.write(string.format("Running task from '%s' with {%s}:\n", + filename, table.concat(run_args, ", "))) + +require(filename).run(run_args) |
