aboutsummaryrefslogtreecommitdiff
path: root/challenge-085
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-11-08 00:12:59 +0000
committerGitHub <noreply@github.com>2020-11-08 00:12:59 +0000
commitf63e8184aded93da0a117d8ec555fc603c3c34cd (patch)
treeb5636aa66968365674ae89d8c12918dac60a7a7b /challenge-085
parent7bf55901177b595e924bd939b15318063ce71c92 (diff)
parentc922378a3971e2cc4db090a618cdbaf497a00a2a (diff)
downloadperlweeklychallenge-club-f63e8184aded93da0a117d8ec555fc603c3c34cd.tar.gz
perlweeklychallenge-club-f63e8184aded93da0a117d8ec555fc603c3c34cd.tar.bz2
perlweeklychallenge-club-f63e8184aded93da0a117d8ec555fc603c3c34cd.zip
Merge pull request #2715 from tylerw/tw/challenge-085
Challenge 085 in Clojure and Lua
Diffstat (limited to 'challenge-085')
-rw-r--r--challenge-085/tyler-wardhaugh/clojure/README.md10
-rw-r--r--challenge-085/tyler-wardhaugh/clojure/deps.edn4
-rw-r--r--challenge-085/tyler-wardhaugh/clojure/pom.xml19
-rw-r--r--challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/core.clj12
-rw-r--r--challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/t1.clj26
-rw-r--r--challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/t2.clj29
-rw-r--r--challenge-085/tyler-wardhaugh/clojure/test/tw/weekly/c85_test.clj22
-rw-r--r--challenge-085/tyler-wardhaugh/lua/README.md4
-rwxr-xr-xchallenge-085/tyler-wardhaugh/lua/ch-1.lua40
-rwxr-xr-xchallenge-085/tyler-wardhaugh/lua/ch-2.lua22
-rwxr-xr-xchallenge-085/tyler-wardhaugh/lua/run.lua9
-rwxr-xr-xchallenge-085/tyler-wardhaugh/lua/test.lua29
12 files changed, 205 insertions, 21 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..71a0090da5 100644
--- a/challenge-085/tyler-wardhaugh/clojure/deps.edn
+++ b/challenge-085/tyler-wardhaugh/clojure/deps.edn
@@ -1,7 +1,7 @@
{: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"}}
+ 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/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 @@
<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.c84</artifactId>
+ <artifactId>tw.weekly.c85</artifactId>
<version>0.1.0-SNAPSHOT</version>
- <name>tw.weekly.c84</name>
- <description>Challenge #084</description>
- <url>https://github.com/tw.weekly/tw.weekly.c84</url>
+ <name>tw.weekly.c85</name>
+ <description>Challenge #085</description>
+ <url>https://github.com/tw.weekly/tw.weekly.c85</url>
<licenses>
<license>
<name>Eclipse Public License</name>
@@ -19,9 +19,9 @@
</developer>
</developers>
<scm>
- <url>https://github.com/tw.weekly/tw.weekly.c84</url>
- <connection>scm:git:git://github.com/tw.weekly/tw.weekly.c84.git</connection>
- <developerConnection>scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c84.git</developerConnection>
+ <url>https://github.com/tw.weekly/tw.weekly.c85</url>
+ <connection>scm:git:git://github.com/tw.weekly/tw.weekly.c85.git</connection>
+ <developerConnection>scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c85.git</developerConnection>
<tag>HEAD</tag>
</scm>
<dependencies>
@@ -30,11 +30,6 @@
<artifactId>clojure</artifactId>
<version>1.10.1</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-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..5e6b7e6dfe
--- /dev/null
+++ b/challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/core.clj
@@ -0,0 +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)
+ (println "\nTask #2:")
+ (t2/-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/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
new file mode 100644
index 0000000000..381c60475e
--- /dev/null
+++ b/challenge-085/tyler-wardhaugh/clojure/test/tw/weekly/c85_test.clj
@@ -0,0 +1,22 @@
+(ns tw.weekly.c85-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [tw.weekly.c85.t1 :refer [find-triplet-sum]]
+ [tw.weekly.c85.t2 :refer [has-power-expr]]))
+
+(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))))))))
+
+(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)))))
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):
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/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/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..fdac1e33d8
--- /dev/null
+++ b/challenge-085/tyler-wardhaugh/lua/test.lua
@@ -0,0 +1,29 @@
+#!/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)
+
+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)