diff options
| -rw-r--r-- | challenge-090/tyler-wardhaugh/clojure/README.md | 10 | ||||
| -rw-r--r-- | challenge-090/tyler-wardhaugh/clojure/deps.edn | 10 | ||||
| -rw-r--r-- | challenge-090/tyler-wardhaugh/clojure/pom.xml | 24 | ||||
| -rw-r--r-- | challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/core.clj | 12 | ||||
| -rw-r--r-- | challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/t1.clj | 28 | ||||
| -rw-r--r-- | challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/t2.clj | 22 | ||||
| -rw-r--r-- | challenge-090/tyler-wardhaugh/clojure/test/tw/weekly/c90_test.clj | 16 | ||||
| -rw-r--r-- | challenge-090/tyler-wardhaugh/lua/README.md | 6 | ||||
| -rwxr-xr-x | challenge-090/tyler-wardhaugh/lua/ch-1.lua | 35 | ||||
| -rwxr-xr-x | challenge-090/tyler-wardhaugh/lua/ch-2.lua | 27 | ||||
| -rwxr-xr-x | challenge-090/tyler-wardhaugh/lua/run.lua | 9 | ||||
| -rwxr-xr-x | challenge-090/tyler-wardhaugh/lua/test.lua | 21 | ||||
| -rw-r--r-- | challenge-090/tyler-wardhaugh/lua/util.lua | 14 |
13 files changed, 202 insertions, 32 deletions
diff --git a/challenge-090/tyler-wardhaugh/clojure/README.md b/challenge-090/tyler-wardhaugh/clojure/README.md index f5a89af18d..41000b8199 100644 --- a/challenge-090/tyler-wardhaugh/clojure/README.md +++ b/challenge-090/tyler-wardhaugh/clojure/README.md @@ -1,13 +1,13 @@ -# tw.weekly.c89 +# tw.weekly.c90 -The Weekly Challenge - #089 - Tyler Wardhaugh +The Weekly Challenge - #090 - Tyler Wardhaugh ## Usage Run the project directly (shows default output from both tasks): - $ clojure -M -m tw.weekly.c89.core + $ clojure -M -m tw.weekly.c90.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.c89.t1 N + $ clojure -M -m tw.weekly.c90.t1 DNA Run Task #2: - $ clojure -M -m tw.weekly.c89.t2 + $ clojure -M -m tw.weekly.c90.t2 A B ## Project Template diff --git a/challenge-090/tyler-wardhaugh/clojure/deps.edn b/challenge-090/tyler-wardhaugh/clojure/deps.edn index cfd5d782d1..1fae9ef933 100644 --- a/challenge-090/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-090/tyler-wardhaugh/clojure/deps.edn @@ -1,9 +1,5 @@ {:paths ["src" "resources"] - :deps {org.clojure/clojure {:mvn/version "1.10.1"} - org.clojure/core.logic {:mvn/version "1.0.0"} - org.clojure/math.combinatorics {:mvn/version "0.1.6"} - org.clojure/math.numeric-tower {:mvn/version "0.0.4"} - org.clojure/tools.macro {:mvn/version "0.1.2"}} + :deps {org.clojure/clojure {:mvn/version "1.10.1"}} :aliases {:test {:extra-paths ["test"] :extra-deps {org.clojure/test.check {:mvn/version "1.0.0"}}} @@ -14,5 +10,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.c88.jar" - "-C" "-m" "tw.weekly.c88"]}}} + :main-opts ["-m" "hf.depstar.uberjar" "tw.weekly.c90.jar" + "-C" "-m" "tw.weekly.c90"]}}} diff --git a/challenge-090/tyler-wardhaugh/clojure/pom.xml b/challenge-090/tyler-wardhaugh/clojure/pom.xml index 3097f51f8e..bc8a82e4cd 100644 --- a/challenge-090/tyler-wardhaugh/clojure/pom.xml +++ b/challenge-090/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.c89</artifactId> + <artifactId>tw.weekly.c90</artifactId> <version>0.1.0-SNAPSHOT</version> - <name>tw.weekly.c89</name> - <description>Challenge #089</description> - <url>https://github.com/tw.weekly/tw.weekly.c89</url> + <name>tw.weekly.c90</name> + <description>Challenge #090</description> + <url>https://github.com/tw.weekly/tw.weekly.c90</url> <licenses> <license> <name>Eclipse Public License</name> @@ -19,9 +19,9 @@ </developer> </developers> <scm> - <url>https://github.com/tw.weekly/tw.weekly.c89</url> - <connection>scm:git:git://github.com/tw.weekly/tw.weekly.c89.git</connection> - <developerConnection>scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c89.git</developerConnection> + <url>https://github.com/tw.weekly/tw.weekly.c90</url> + <connection>scm:git:git://github.com/tw.weekly/tw.weekly.c90.git</connection> + <developerConnection>scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c90.git</developerConnection> <tag>HEAD</tag> </scm> <dependencies> @@ -30,16 +30,6 @@ <artifactId>clojure</artifactId> <version>1.10.1</version> </dependency> - <dependency> - <groupId>net.mikera</groupId> - <artifactId>core.matrix</artifactId> - <version>0.62.0</version> - </dependency> - <dependency> - <groupId>org.clojure</groupId> - <artifactId>math.numeric-tower</artifactId> - <version>0.0.4</version> - </dependency> </dependencies> <build> <sourceDirectory>src</sourceDirectory> diff --git a/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/core.clj b/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/core.clj new file mode 100644 index 0000000000..296c2c6051 --- /dev/null +++ b/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/core.clj @@ -0,0 +1,12 @@ +(ns tw.weekly.c90.core + (:require [tw.weekly.c90.t1 :as t1]) + (:require [tw.weekly.c90.t2 :as t2]) + (:gen-class)) + +(defn -main + "Run all tasks" + [& _] + (println "Task #1:") + (t1/-main) + (println "\nTask #2:") + (t2/-main)) diff --git a/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/t1.clj b/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/t1.clj new file mode 100644 index 0000000000..e739241345 --- /dev/null +++ b/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/t1.clj @@ -0,0 +1,28 @@ +(ns tw.weekly.c90.t1 + (:require [clojure.string :as str]) + (:require [clojure.pprint :refer [cl-format]]) + (:require [clojure.edn :as edn])) + +;;; +; Task description for TASK #1 › DNA Sequence +;;; + +(def DEFAULT-DNA + "GTAAACCCCTTTTCATTTAGACAGATCGACTCCTTATCCATTCTCAGAGATGTGTTGCTGGTCGCCG") + +(defn process-dna + "Process the DNA sequence, counting nucleiobases and determining the + complementary sequence." + [dna] + (let [complement-dna (fn [s] (str/escape s {\T \A, \A \T, \C \G, \G \C}))] + ((juxt frequencies complement-dna) dna))) + +(defn -main + "Run Task 1 with a given DNA sequence, defaulting to the example given in + the task description." + [& args] + (let [dna (or (some-> args first edn/read-string) DEFAULT-DNA) + [freqs complement-seq] (process-dna dna)] + (cl-format true "~12a: ~a~%~12a: ~a~%" + "Counts" freqs + "Complement" complement-seq))) diff --git a/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/t2.clj b/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/t2.clj new file mode 100644 index 0000000000..ab767f91fc --- /dev/null +++ b/challenge-090/tyler-wardhaugh/clojure/src/tw/weekly/c90/t2.clj @@ -0,0 +1,22 @@ +(ns tw.weekly.c90.t2 + (:require [clojure.edn :as edn])) + +;;; +; Task description for TASK #2 › Ethiopian Multiplication +;;; + +(defn ethiopian-multiply + "Compute the product of two positive integers using Ethiopian Multiplication." + [a b] + (let [source (map vector (iterate #(quot % 2) a) (iterate #(* 2 %) b)) + xf (comp + (take-while (fn [[a _]] (pos? a))) + (keep (fn [[a b]] (when (odd? a) b))))] + (transduce xf + source))) + +(defn -main + "Run Task 2 with the given positive integers, defaulting to the example + given in the explanation page linked from the task description." + [& args] + (let [[A B] (or (some->> args (take 2) (map edn/read-string)) [12 14])] + (println (ethiopian-multiply A B)))) diff --git a/challenge-090/tyler-wardhaugh/clojure/test/tw/weekly/c90_test.clj b/challenge-090/tyler-wardhaugh/clojure/test/tw/weekly/c90_test.clj new file mode 100644 index 0000000000..a09788f137 --- /dev/null +++ b/challenge-090/tyler-wardhaugh/clojure/test/tw/weekly/c90_test.clj @@ -0,0 +1,16 @@ +(ns tw.weekly.c90-test + (:require [clojure.test :refer [deftest is testing]] + [tw.weekly.c90.t1 :refer [DEFAULT-DNA process-dna]] + [tw.weekly.c90.t2 :refer [ethiopian-multiply]])) + +(def COMPLEMENT-DEFAULT-DNA + "CATTTGGGGAAAAGTAAATCTGTCTAGCTGAGGAATAGGTAAGAGTCTCTACACAACGACCAGCGGC") + +(deftest task-1 + (testing "Task 1, DNA Sequence" + (is (= [{\G 13, \T 22, \A 14, \C 18}, COMPLEMENT-DEFAULT-DNA] + (process-dna DEFAULT-DNA))))) + +(deftest task-2 + (testing "Task 2, Ethiopian Multiplication" + (is (= 168 (ethiopian-multiply 12 14))))) diff --git a/challenge-090/tyler-wardhaugh/lua/README.md b/challenge-090/tyler-wardhaugh/lua/README.md index d2f91b15fe..8772dac9c1 100644 --- a/challenge-090/tyler-wardhaugh/lua/README.md +++ b/challenge-090/tyler-wardhaugh/lua/README.md @@ -1,17 +1,17 @@ # The Weekly Challenge -The Weekly Challenge - #089 - Tyler Wardhaugh +The Weekly Challenge - #090 - Tyler Wardhaugh ## Usage Run Task 1: - $ ./run.lua ch-1 N + $ ./run.lua ch-1 DNA Run Task 2: - $ ./run.lua ch-2 MATRIX-FILE + $ ./run.lua ch-2 A B Run the project's tests (all the samples from the task descriptions plus some others): diff --git a/challenge-090/tyler-wardhaugh/lua/ch-1.lua b/challenge-090/tyler-wardhaugh/lua/ch-1.lua new file mode 100755 index 0000000000..e964ce9f6d --- /dev/null +++ b/challenge-090/tyler-wardhaugh/lua/ch-1.lua @@ -0,0 +1,35 @@ +#!/usr/bin/env lua + +local t1 = {} +local util = require'util' + +t1.DEFAULT_DNA = 'GTAAACCCCTTTTCATTTAGACAGATCGACTCCTTATCCATTCTCAGAGATGTGTTGCTGGTCGCCG' + +function t1.process_dna(dna) + freq = {} + for c in dna:gmatch'.' do + freq[c] = (freq[c] or 0) + 1 + end + + local replacement = {A='T', T='A', C='G', G='C'} + local complement = dna:gsub('(%w)', function (c) return replacement[c] end) + + return freq, complement +end + +function t1.run(args) + local dna = args[1] or t1.DEFAULT_DNA + local freq, complement = t1.process_dna(dna) + + io.write('Counts = { ') + local seq = util.dict_to_seq(freq) + for i, v in ipairs(seq) do + io.write(v[1] .. ': ' .. v[2]) + if i ~= #seq then io.write(', ') end + end + print(' }\n') + + print('Complement: ' .. complement) +end + +return t1 diff --git a/challenge-090/tyler-wardhaugh/lua/ch-2.lua b/challenge-090/tyler-wardhaugh/lua/ch-2.lua new file mode 100755 index 0000000000..85c69b7be8 --- /dev/null +++ b/challenge-090/tyler-wardhaugh/lua/ch-2.lua @@ -0,0 +1,27 @@ +#!/usr/bin/env lua + +local t2 = {} + +function t2.ethiopian_multiply(a, b) + local product = 0 + + while a > 0 do + if a % 2 ~= 0 then + product = product + b + end + + a = a // 2 + b = b * 2 + end + + return product +end + +function t2.run(args) + local A = tonumber(args[1]) or 12 + local B = tonumber(args[2]) or 14 + local product = t2.ethiopian_multiply(A, B) + print(product) +end + +return t2 diff --git a/challenge-090/tyler-wardhaugh/lua/run.lua b/challenge-090/tyler-wardhaugh/lua/run.lua new file mode 100755 index 0000000000..c6e7473bee --- /dev/null +++ b/challenge-090/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) diff --git a/challenge-090/tyler-wardhaugh/lua/test.lua b/challenge-090/tyler-wardhaugh/lua/test.lua new file mode 100755 index 0000000000..7dffde2f53 --- /dev/null +++ b/challenge-090/tyler-wardhaugh/lua/test.lua @@ -0,0 +1,21 @@ +#!/usr/bin/env lua + +require 'busted.runner'() + +describe("Task 1, DNA Sequence", function() + local t1 = require'ch-1' + it("produces correct results for the examples", function() + local freq, complement = t1.process_dna(t1.DEFAULT_DNA) + assert.are.same({G=13, T=22, A=14, C=18 }, freq) + assert.are.same('CATTTGGGGAAAAGTAAATCTGTCTAGCTGAGGAATAGGTAAGAGTCTCTACACAACGACCAGCGGC', + complement) + end) +end) + +describe("Task 2, Ethiopian Multiplication", function() + local t2 = require'ch-2' + it("produces correct results for the examples", function() + assert.are.same(168, t2.ethiopian_multiply(12, 14)) + assert.are.same(1554, t2.ethiopian_multiply(37, 42)) + end) +end) diff --git a/challenge-090/tyler-wardhaugh/lua/util.lua b/challenge-090/tyler-wardhaugh/lua/util.lua new file mode 100644 index 0000000000..1623b04824 --- /dev/null +++ b/challenge-090/tyler-wardhaugh/lua/util.lua @@ -0,0 +1,14 @@ +#!/usr/bin/env lua + +util = {} + +--[[ table ]]-- +function util.dict_to_seq(coll) + local result = {} + for k, v in pairs(coll) do + table.insert(result, { k, v }) + end + return result +end + +return util |
