From 36a12585aadecc449f49480df9dba163e7bee542 Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Mon, 2 Nov 2020 13:22:14 -0800 Subject: Ch85: prep for challenge --- challenge-085/tyler-wardhaugh/clojure/README.md | 10 +++++----- challenge-085/tyler-wardhaugh/clojure/deps.edn | 4 +--- challenge-085/tyler-wardhaugh/clojure/pom.xml | 19 +++++++------------ challenge-085/tyler-wardhaugh/lua/README.md | 4 ++-- 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/challenge-085/tyler-wardhaugh/clojure/README.md b/challenge-085/tyler-wardhaugh/clojure/README.md index 8e01332e5c..32d800bdc9 100644 --- a/challenge-085/tyler-wardhaugh/clojure/README.md +++ b/challenge-085/tyler-wardhaugh/clojure/README.md @@ -1,13 +1,13 @@ -# tw.weekly.c84 +# tw.weekly.c85 -The Weekly Challenge - #082 - Tyler Wardhaugh +The Weekly Challenge - #085 - Tyler Wardhaugh ## Usage Run the project directly (shows default output from both tasks): - $ clojure -M -m tw.weekly.c84.core + $ clojure -M -m tw.weekly.c85.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.c84.t1 N + $ clojure -M -m tw.weekly.c85.t1 A1 A2 A3... Run Task #2 with input: - $ clojure -M -m tw.weekly.c84.t2 MATRIX-FILE + $ clojure -M -m tw.weekly.c85.t2 N ## Project Template diff --git a/challenge-085/tyler-wardhaugh/clojure/deps.edn b/challenge-085/tyler-wardhaugh/clojure/deps.edn index 5b012f1be6..cea4fb663c 100644 --- a/challenge-085/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-085/tyler-wardhaugh/clojure/deps.edn @@ -1,7 +1,5 @@ {:paths ["src" "resources"] - :deps {org.clojure/clojure {:mvn/version "1.10.1"} - net.cgrand/xforms {:mvn/version "0.19.2"} - net.mikera/core.matrix {:mvn/version "0.62.0"}} + :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"}}} diff --git a/challenge-085/tyler-wardhaugh/clojure/pom.xml b/challenge-085/tyler-wardhaugh/clojure/pom.xml index 3f619ed7d4..75dbd11d47 100644 --- a/challenge-085/tyler-wardhaugh/clojure/pom.xml +++ b/challenge-085/tyler-wardhaugh/clojure/pom.xml @@ -2,11 +2,11 @@ 4.0.0 tw.weekly - tw.weekly.c84 + tw.weekly.c85 0.1.0-SNAPSHOT - tw.weekly.c84 - Challenge #084 - https://github.com/tw.weekly/tw.weekly.c84 + tw.weekly.c85 + Challenge #085 + https://github.com/tw.weekly/tw.weekly.c85 Eclipse Public License @@ -19,9 +19,9 @@ - https://github.com/tw.weekly/tw.weekly.c84 - scm:git:git://github.com/tw.weekly/tw.weekly.c84.git - scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c84.git + https://github.com/tw.weekly/tw.weekly.c85 + scm:git:git://github.com/tw.weekly/tw.weekly.c85.git + scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c85.git HEAD @@ -30,11 +30,6 @@ clojure 1.10.1 - - org.clojure - math.numeric-tower - 0.0.4 - src diff --git a/challenge-085/tyler-wardhaugh/lua/README.md b/challenge-085/tyler-wardhaugh/lua/README.md index 39b6e1243e..8c4c54f316 100644 --- a/challenge-085/tyler-wardhaugh/lua/README.md +++ b/challenge-085/tyler-wardhaugh/lua/README.md @@ -7,11 +7,11 @@ The Weekly Challenge - #084 - Tyler Wardhaugh Run Task 1: - $ ./run.lua ch-1 N + $ ./run.lua ch-1 A1 A2 A3... Run Task 2: - $ ./run.lua ch-2 MATRIX-FILE + $ ./run.lua ch-2 N Run the project's tests (all the samples from the task descriptions plus some others): -- cgit From dd1ff820ba0d044cbef420504a292ec5aa3057d5 Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Mon, 2 Nov 2020 16:25:27 -0800 Subject: Ch85 (Clojure): Task 1 --- challenge-085/tyler-wardhaugh/clojure/deps.edn | 3 ++- .../clojure/src/tw/weekly/c85/core.clj | 9 ++++++++ .../clojure/src/tw/weekly/c85/t1.clj | 26 ++++++++++++++++++++++ .../clojure/test/tw/weekly/c85_test.clj | 15 +++++++++++++ 4 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/core.clj create mode 100644 challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/t1.clj create mode 100644 challenge-085/tyler-wardhaugh/clojure/test/tw/weekly/c85_test.clj diff --git a/challenge-085/tyler-wardhaugh/clojure/deps.edn b/challenge-085/tyler-wardhaugh/clojure/deps.edn index cea4fb663c..cdbf046c18 100644 --- a/challenge-085/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-085/tyler-wardhaugh/clojure/deps.edn @@ -1,5 +1,6 @@ {:paths ["src" "resources"] - :deps {org.clojure/clojure {:mvn/version "1.10.1"}} + :deps {org.clojure/clojure {:mvn/version "1.10.1"} + org.clojure/math.combinatorics {:mvn/version "0.1.6"}} :aliases {:test {:extra-paths ["test"] :extra-deps {org.clojure/test.check {:mvn/version "1.0.0"}}} diff --git a/challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/core.clj b/challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/core.clj new file mode 100644 index 0000000000..26b82079ac --- /dev/null +++ b/challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/core.clj @@ -0,0 +1,9 @@ +(ns tw.weekly.c85.core + (:require [tw.weekly.c85.t1 :as t1]) + (:gen-class)) + +(defn -main + "Run all tasks" + [& _] + (println "Task #1:") + (t1/-main)) diff --git a/challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/t1.clj b/challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/t1.clj new file mode 100644 index 0000000000..e2b6cad34b --- /dev/null +++ b/challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/t1.clj @@ -0,0 +1,26 @@ +(ns tw.weekly.c85.t1 + (:require [clojure.math.combinatorics :as combo]) + (:require [clojure.edn :as edn])) + +;;; +; Task description for TASK #1 › Triplet Sum +;;; + +(defn find-triplet-sum + "Find a triplet such that 1 < a+b+c < 2. If found, return the smallest + sum, otherwise nil." + [coll] + (let [lower 1, upper 2, num-items 3 + expand (fn [[e cnt]] (repeat (min num-items cnt) e)) + v (->> coll (filter (partial > upper)) frequencies (mapcat expand))] + (->> (combo/permuted-combinations v num-items) + (map (partial reduce +)) + (filter #(< lower % upper)) + first))) + +(defn -main + "Run Task 1 with an list of positive real numbers R, defaulting to the + first example given in the task description." + [& args] + (let [R (or (some->> args (map edn/read-string)) [1.2 0.4 0.1 2.5])] + (println (if (find-triplet-sum R) 1 0)))) diff --git a/challenge-085/tyler-wardhaugh/clojure/test/tw/weekly/c85_test.clj b/challenge-085/tyler-wardhaugh/clojure/test/tw/weekly/c85_test.clj new file mode 100644 index 0000000000..49c495a2cb --- /dev/null +++ b/challenge-085/tyler-wardhaugh/clojure/test/tw/weekly/c85_test.clj @@ -0,0 +1,15 @@ +(ns tw.weekly.c85-test + (:require [clojure.test :refer [deftest is testing]] + [tw.weekly.c85.t1 :refer [find-triplet-sum]])) + +(deftest task-1 + (testing "Task 1, Triplet Sum" + (is (some? (find-triplet-sum [1.2 0.4 0.1 2.5]))) + (is (nil? (find-triplet-sum [0.2 1.5 0.9 1.1]))) + (is (some? (find-triplet-sum [0.5 1.1 0.3 0.7]))) + + (is (nil? (find-triplet-sum [0 0.5 0.5]))) + (is (nil? (find-triplet-sum [0.1 0.2 0.3]))) + (is (nil? (find-triplet-sum [1.1 0.1]))) + (is (some? (find-triplet-sum [0.2 0.5 0.5 0.5 0.8 0.8]))) + (is (some? (find-triplet-sum (concat [0.9 0.8 0.05] (repeat 100 (rand-int 1000)))))))) -- cgit From c3dbe3a5ada81faffaff12f97b2c073012aa3778 Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Mon, 2 Nov 2020 18:11:29 -0800 Subject: Ch85 (Clojure): Task 2 --- challenge-085/tyler-wardhaugh/clojure/deps.edn | 3 ++- .../clojure/src/tw/weekly/c85/core.clj | 5 +++- .../clojure/src/tw/weekly/c85/t2.clj | 29 ++++++++++++++++++++++ .../clojure/test/tw/weekly/c85_test.clj | 9 ++++++- 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/t2.clj diff --git a/challenge-085/tyler-wardhaugh/clojure/deps.edn b/challenge-085/tyler-wardhaugh/clojure/deps.edn index cdbf046c18..71a0090da5 100644 --- a/challenge-085/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-085/tyler-wardhaugh/clojure/deps.edn @@ -1,6 +1,7 @@ {:paths ["src" "resources"] :deps {org.clojure/clojure {:mvn/version "1.10.1"} - org.clojure/math.combinatorics {:mvn/version "0.1.6"}} + org.clojure/math.combinatorics {:mvn/version "0.1.6"} + 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"}}} diff --git a/challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/core.clj b/challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/core.clj index 26b82079ac..5e6b7e6dfe 100644 --- a/challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/core.clj +++ b/challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/core.clj @@ -1,9 +1,12 @@ (ns tw.weekly.c85.core (:require [tw.weekly.c85.t1 :as t1]) + (:require [tw.weekly.c85.t2 :as t2]) (:gen-class)) (defn -main "Run all tasks" [& _] (println "Task #1:") - (t1/-main)) + (t1/-main) + (println "\nTask #2:") + (t2/-main)) diff --git a/challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/t2.clj b/challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/t2.clj new file mode 100644 index 0000000000..033f493329 --- /dev/null +++ b/challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/t2.clj @@ -0,0 +1,29 @@ +(ns tw.weekly.c85.t2 + (:require [clojure.edn :as edn]) + (:require [clojure.math.numeric-tower :as math])) + +;;; +; Task description for TASK #2 › Power of Two Integers +;;; + +(defn logN + "Compute the log of x in base y." + [x y] + (/ (Math/log x) (Math/log y))) + +(defn has-power-expr + "Find one or more [a b] such that n = a ^ b where a > 0 and b > 1. If no + expression is possible, nil is returned." + [n] + (let [endpoint (fn [x] (inc (math/floor (logN n x)))) + combos (for [a (range 2 (endpoint 2)) + b (range 2 (endpoint a))] + (math/expt a b))] + (->> combos (filter (partial = n)) first))) + +(defn -main + "Run Task 2 with a positive integer, defaulting to the example given in the task description." + [& args] + (let [N (or (some-> args first edn/read-string) 8) + result (has-power-expr N)] + (println (if result 1 0)))) diff --git a/challenge-085/tyler-wardhaugh/clojure/test/tw/weekly/c85_test.clj b/challenge-085/tyler-wardhaugh/clojure/test/tw/weekly/c85_test.clj index 49c495a2cb..381c60475e 100644 --- a/challenge-085/tyler-wardhaugh/clojure/test/tw/weekly/c85_test.clj +++ b/challenge-085/tyler-wardhaugh/clojure/test/tw/weekly/c85_test.clj @@ -1,6 +1,7 @@ (ns tw.weekly.c85-test (:require [clojure.test :refer [deftest is testing]] - [tw.weekly.c85.t1 :refer [find-triplet-sum]])) + [tw.weekly.c85.t1 :refer [find-triplet-sum]] + [tw.weekly.c85.t2 :refer [has-power-expr]])) (deftest task-1 (testing "Task 1, Triplet Sum" @@ -13,3 +14,9 @@ (is (nil? (find-triplet-sum [1.1 0.1]))) (is (some? (find-triplet-sum [0.2 0.5 0.5 0.5 0.8 0.8]))) (is (some? (find-triplet-sum (concat [0.9 0.8 0.05] (repeat 100 (rand-int 1000)))))))) + +(deftest task-2 + (testing "Task 2, Power of Two Integers" + (is (some? (has-power-expr 8))) + (is (nil? (has-power-expr 15))) + (is (some? (has-power-expr 125))))) -- cgit From 0e48f28c59973db242fb582595c59f7eadb89493 Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Thu, 5 Nov 2020 14:51:24 -0800 Subject: Ch85 (Lua): Task 1 --- challenge-085/tyler-wardhaugh/lua/ch-1.lua | 40 ++++++++++++++++++++++++++++++ challenge-085/tyler-wardhaugh/lua/run.lua | 9 +++++++ challenge-085/tyler-wardhaugh/lua/test.lua | 20 +++++++++++++++ 3 files changed, 69 insertions(+) create mode 100755 challenge-085/tyler-wardhaugh/lua/ch-1.lua create mode 100755 challenge-085/tyler-wardhaugh/lua/run.lua create mode 100755 challenge-085/tyler-wardhaugh/lua/test.lua diff --git a/challenge-085/tyler-wardhaugh/lua/ch-1.lua b/challenge-085/tyler-wardhaugh/lua/ch-1.lua new file mode 100755 index 0000000000..d6be13973d --- /dev/null +++ b/challenge-085/tyler-wardhaugh/lua/ch-1.lua @@ -0,0 +1,40 @@ +#!/usr/bin/env lua + +local t1 = {} + +function t1.find_triplet_sum(coll) + local lower = 1 + local upper = 2 + local num_elems = 3 + + -- filter out any value more than the upper bound and limit repeated values + -- to the number of elements we sum (3) + local seen = {} + local tbl = {} + for _, v in ipairs(coll) do + seen[v] = 1 + (seen[v] or 0) + if (v < upper) and (seen[v] <= num_elems) then table.insert(tbl, v) end + end + + local sum + for i = 1,#tbl - 2 do + for j = i+1,#tbl - 1 do + for k = j+1,#tbl do + sum = tbl[i] + tbl[j] + tbl[k] + if (lower < sum) and (sum < upper) then + return 1 + end + end + end + end + + return 0 +end + +function t1.run(args) + local coll = {} + for _, v in ipairs(args) do table.insert(coll, tonumber(v)) end + print(t1.find_triplet_sum(coll)) +end + +return t1 diff --git a/challenge-085/tyler-wardhaugh/lua/run.lua b/challenge-085/tyler-wardhaugh/lua/run.lua new file mode 100755 index 0000000000..c6e7473bee --- /dev/null +++ b/challenge-085/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-085/tyler-wardhaugh/lua/test.lua b/challenge-085/tyler-wardhaugh/lua/test.lua new file mode 100755 index 0000000000..4577757e03 --- /dev/null +++ b/challenge-085/tyler-wardhaugh/lua/test.lua @@ -0,0 +1,20 @@ +#!/usr/bin/env lua + +require 'busted.runner'() + +describe("Task 1, Triplet Sum", function() + local t1 = require'ch-1' + it("produces correct results for the examples", function() + assert.are.same(1, t1.find_triplet_sum({1.2, 0.4, 0.1, 2.5})) + assert.are.same(0, t1.find_triplet_sum({0.2, 1.5, 0.9, 1.1})) + assert.are.same(1, t1.find_triplet_sum({0.5, 1.1, 0.3, 0.7})) + + assert.are.same(0, t1.find_triplet_sum({0, 0.5, 0.5})) + assert.are.same(0, t1.find_triplet_sum({0.1, 0.2, 0.3})) + assert.are.same(0, t1.find_triplet_sum({1.1, 0.1})) + + local big = {0.9, 0.8, 0.05} + for _ = 1,100 do table.insert(big, math.random(2, 1000)) end + assert.are.same(1, t1.find_triplet_sum(big)) + end) +end) -- cgit From c922378a3971e2cc4db090a618cdbaf497a00a2a Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Thu, 5 Nov 2020 14:51:44 -0800 Subject: Ch85 (Lua): Task 2 --- challenge-085/tyler-wardhaugh/lua/ch-2.lua | 22 ++++++++++++++++++++++ challenge-085/tyler-wardhaugh/lua/test.lua | 9 +++++++++ 2 files changed, 31 insertions(+) create mode 100755 challenge-085/tyler-wardhaugh/lua/ch-2.lua diff --git a/challenge-085/tyler-wardhaugh/lua/ch-2.lua b/challenge-085/tyler-wardhaugh/lua/ch-2.lua new file mode 100755 index 0000000000..6af287ab8d --- /dev/null +++ b/challenge-085/tyler-wardhaugh/lua/ch-2.lua @@ -0,0 +1,22 @@ +#!/usr/bin/env lua + +local t2 = {} + +function t2.has_power_expr(n) + local endpoint = function(x) return 1 + math.floor(math.log(n, x)) end + for a = 2,endpoint(2) do + for b = 2,endpoint(a) do + if n == a ^ b then + return 1 + end + end + end + + return 0 +end + +function t2.run(args) + print(t2.has_power_expr(tonumber(args[1]))) +end + +return t2 diff --git a/challenge-085/tyler-wardhaugh/lua/test.lua b/challenge-085/tyler-wardhaugh/lua/test.lua index 4577757e03..fdac1e33d8 100755 --- a/challenge-085/tyler-wardhaugh/lua/test.lua +++ b/challenge-085/tyler-wardhaugh/lua/test.lua @@ -18,3 +18,12 @@ describe("Task 1, Triplet Sum", function() assert.are.same(1, t1.find_triplet_sum(big)) end) end) + +describe("Task 2, Power of Two Integers", function() + local t2 = require'ch-2' + it("produces correct results for the examples", function() + assert.are.same(1, t2.has_power_expr(8)) + assert.are.same(0, t2.has_power_expr(15)) + assert.are.same(1, t2.has_power_expr(125)) + end) +end) -- cgit