diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-12-18 22:39:33 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-12-18 22:39:33 +0000 |
| commit | 511f2e58d09f8d26f251a95c282da9d7b4be9b39 (patch) | |
| tree | 023570136fcefd5d76e168195971ad7afebeccd8 | |
| parent | 42bbd7ccecb01b8550e34d68fd3af05fc7bdc26a (diff) | |
| parent | a395742fbe5e2605110c5fa4ee1f8a83e5c8173b (diff) | |
| download | perlweeklychallenge-club-511f2e58d09f8d26f251a95c282da9d7b4be9b39.tar.gz perlweeklychallenge-club-511f2e58d09f8d26f251a95c282da9d7b4be9b39.tar.bz2 perlweeklychallenge-club-511f2e58d09f8d26f251a95c282da9d7b4be9b39.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
| -rw-r--r-- | challenge-091/tyler-wardhaugh/clojure/README.md | 10 | ||||
| -rw-r--r-- | challenge-091/tyler-wardhaugh/clojure/deps.edn | 4 | ||||
| -rw-r--r-- | challenge-091/tyler-wardhaugh/clojure/pom.xml | 14 | ||||
| -rw-r--r-- | challenge-091/tyler-wardhaugh/clojure/src/tw/weekly/c91/core.clj | 12 | ||||
| -rw-r--r-- | challenge-091/tyler-wardhaugh/clojure/src/tw/weekly/c91/t1.clj | 27 | ||||
| -rw-r--r-- | challenge-091/tyler-wardhaugh/clojure/src/tw/weekly/c91/t2.clj | 30 | ||||
| -rw-r--r-- | challenge-091/tyler-wardhaugh/clojure/test/tw/weekly/c91_test.clj | 15 | ||||
| -rw-r--r-- | challenge-091/tyler-wardhaugh/lua/README.md | 6 | ||||
| -rwxr-xr-x | challenge-091/tyler-wardhaugh/lua/ch-1.lua | 26 | ||||
| -rwxr-xr-x | challenge-091/tyler-wardhaugh/lua/ch-2.lua | 35 | ||||
| -rwxr-xr-x | challenge-091/tyler-wardhaugh/lua/run.lua | 9 | ||||
| -rwxr-xr-x | challenge-091/tyler-wardhaugh/lua/test.lua | 20 |
12 files changed, 191 insertions, 17 deletions
diff --git a/challenge-091/tyler-wardhaugh/clojure/README.md b/challenge-091/tyler-wardhaugh/clojure/README.md index 41000b8199..32467a13d1 100644 --- a/challenge-091/tyler-wardhaugh/clojure/README.md +++ b/challenge-091/tyler-wardhaugh/clojure/README.md @@ -1,13 +1,13 @@ -# tw.weekly.c90 +# tw.weekly.c91 -The Weekly Challenge - #090 - Tyler Wardhaugh +The Weekly Challenge - #091 - Tyler Wardhaugh ## Usage Run the project directly (shows default output from both tasks): - $ clojure -M -m tw.weekly.c90.core + $ clojure -M -m tw.weekly.c91.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.c90.t1 DNA + $ clojure -M -m tw.weekly.c91.t1 N Run Task #2: - $ clojure -M -m tw.weekly.c90.t2 A B + $ clojure -M -m tw.weekly.c91.t2 N1 N2 N3... ## Project Template diff --git a/challenge-091/tyler-wardhaugh/clojure/deps.edn b/challenge-091/tyler-wardhaugh/clojure/deps.edn index 1fae9ef933..e89f0d9fff 100644 --- a/challenge-091/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-091/tyler-wardhaugh/clojure/deps.edn @@ -10,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.c90.jar" - "-C" "-m" "tw.weekly.c90"]}}} + :main-opts ["-m" "hf.depstar.uberjar" "tw.weekly.c91.jar" + "-C" "-m" "tw.weekly.c91"]}}} diff --git a/challenge-091/tyler-wardhaugh/clojure/pom.xml b/challenge-091/tyler-wardhaugh/clojure/pom.xml index bc8a82e4cd..88c2883d84 100644 --- a/challenge-091/tyler-wardhaugh/clojure/pom.xml +++ b/challenge-091/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.c90</artifactId> + <artifactId>tw.weekly.c91</artifactId> <version>0.1.0-SNAPSHOT</version> - <name>tw.weekly.c90</name> - <description>Challenge #090</description> - <url>https://github.com/tw.weekly/tw.weekly.c90</url> + <name>tw.weekly.c91</name> + <description>Challenge #091</description> + <url>https://github.com/tw.weekly/tw.weekly.c91</url> <licenses> <license> <name>Eclipse Public License</name> @@ -19,9 +19,9 @@ </developer> </developers> <scm> - <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> + <url>https://github.com/tw.weekly/tw.weekly.c91</url> + <connection>scm:git:git://github.com/tw.weekly/tw.weekly.c91.git</connection> + <developerConnection>scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c91.git</developerConnection> <tag>HEAD</tag> </scm> <dependencies> diff --git a/challenge-091/tyler-wardhaugh/clojure/src/tw/weekly/c91/core.clj b/challenge-091/tyler-wardhaugh/clojure/src/tw/weekly/c91/core.clj new file mode 100644 index 0000000000..0076650f33 --- /dev/null +++ b/challenge-091/tyler-wardhaugh/clojure/src/tw/weekly/c91/core.clj @@ -0,0 +1,12 @@ +(ns tw.weekly.c91.core + (:require [tw.weekly.c91.t1 :as t1]) + (:require [tw.weekly.c91.t2 :as t2]) + (:gen-class)) + +(defn -main + "Run all tasks" + [& _] + (println "Task #1:") + (t1/-main) + (println "\nTask #2:") + (t2/-main)) diff --git a/challenge-091/tyler-wardhaugh/clojure/src/tw/weekly/c91/t1.clj b/challenge-091/tyler-wardhaugh/clojure/src/tw/weekly/c91/t1.clj new file mode 100644 index 0000000000..fb9fa9db10 --- /dev/null +++ b/challenge-091/tyler-wardhaugh/clojure/src/tw/weekly/c91/t1.clj @@ -0,0 +1,27 @@ +(ns tw.weekly.c91.t1 + (:require [clojure.pprint :refer [cl-format]]) + (:require [clojure.edn :as edn])) + +;;; +; Task description for TASK #1 › Count Number +;;; + +(def DEFAULT-INPUT 1122234) + +(defn count-number + "Produce a simple run-level encoding for a positive integer." + [n] + (let [xf (mapcat (juxt count first))] + (edn/read-string (transduce xf str (->> n str (partition-by identity)))))) + +(defn -main + "Run Task 1 using a given integer, defaulting to the example given in + the task description." + [& args] + (let [N (or (some-> args first edn/read-string) DEFAULT-INPUT) + result (count-number N)] + (println result) + (cl-format true "~%as we read ~{~{~r ~a~}~^ ~}~%" + (->> (str result) + (partition 2) + (map (fn [[c v]] [(Integer/parseInt (str c)) v])))))) diff --git a/challenge-091/tyler-wardhaugh/clojure/src/tw/weekly/c91/t2.clj b/challenge-091/tyler-wardhaugh/clojure/src/tw/weekly/c91/t2.clj new file mode 100644 index 0000000000..ce6a7d1a9d --- /dev/null +++ b/challenge-091/tyler-wardhaugh/clojure/src/tw/weekly/c91/t2.clj @@ -0,0 +1,30 @@ +(ns tw.weekly.c91.t2 + (:require [clojure.edn :as edn])) + +;;; +; Task description for TASK #2 › Jump Game +;;; + +(def DEFAULT-INPUT [1 2 1 2]) + +(defn jump-game + "Play the jump game with coll, returning true if we can get to the last + number, false otherwise" + [coll] + (loop [v (vec coll) + end (dec (count v)) + i 0] + (if (= i end) + true + (let [j (+ i (v i))] + (cond + (= i j) false + (<= j end) (recur v end j) + :else false))))) + +(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 [N (or (some->> args (map edn/read-string)) DEFAULT-INPUT)] + (println (if (jump-game N) 1 0)))) diff --git a/challenge-091/tyler-wardhaugh/clojure/test/tw/weekly/c91_test.clj b/challenge-091/tyler-wardhaugh/clojure/test/tw/weekly/c91_test.clj new file mode 100644 index 0000000000..274d0e95b4 --- /dev/null +++ b/challenge-091/tyler-wardhaugh/clojure/test/tw/weekly/c91_test.clj @@ -0,0 +1,15 @@ +(ns tw.weekly.c91-test + (:require [clojure.test :refer [deftest is testing]] + [tw.weekly.c91.t1 :refer [count-number]] + [tw.weekly.c91.t2 :refer [jump-game]])) + +(deftest task-1 + (testing "Task 1, Count Number" + (is (= 21321314 (count-number 1122234))) + (is (= 12332415 (count-number 2333445))) + (is (= 1112131415 (count-number 12345))))) + +(deftest task-2 + (testing "Task 2, Jump Game" + (is (true? (jump-game [1 2 1 2]))) + (is (false? (jump-game [2 1 1 0 2]))))) diff --git a/challenge-091/tyler-wardhaugh/lua/README.md b/challenge-091/tyler-wardhaugh/lua/README.md index 8772dac9c1..2e1e278b45 100644 --- a/challenge-091/tyler-wardhaugh/lua/README.md +++ b/challenge-091/tyler-wardhaugh/lua/README.md @@ -1,17 +1,17 @@ # The Weekly Challenge -The Weekly Challenge - #090 - Tyler Wardhaugh +The Weekly Challenge - #091 - Tyler Wardhaugh ## Usage Run Task 1: - $ ./run.lua ch-1 DNA + $ ./run.lua ch-1 N Run Task 2: - $ ./run.lua ch-2 A B + $ ./run.lua ch-2 N1 N2 N3... Run the project's tests (all the samples from the task descriptions plus some others): diff --git a/challenge-091/tyler-wardhaugh/lua/ch-1.lua b/challenge-091/tyler-wardhaugh/lua/ch-1.lua new file mode 100755 index 0000000000..844ba9b32e --- /dev/null +++ b/challenge-091/tyler-wardhaugh/lua/ch-1.lua @@ -0,0 +1,26 @@ +#!/usr/bin/env lua + +local t1 = {} +t1.DEFAULT_INPUT = 1122234 + +function t1.count_numbers(n) + --[[ + Lua's standard pattern matching does not support back-references, so + we need to do some trickery. + + Idea from: + https://stackoverflow.com/questions/48541679/lua-pattern-matching-series-of-same-character + ]]-- + local result = (tostring(n) .. '\0') + :gsub('%d', '\0%0%0') + :gsub('(.)%z%1', '%1') + :gsub('%d+%z', function(run) return run:len() - 2 .. run:sub(1, 1) end) + return tonumber(result:sub(2)) +end + +function t1.run(args) + local n = args[1] or t1.DEFAULT_INPUT + print(t1.count_numbers(n)) +end + +return t1 diff --git a/challenge-091/tyler-wardhaugh/lua/ch-2.lua b/challenge-091/tyler-wardhaugh/lua/ch-2.lua new file mode 100755 index 0000000000..768ced2596 --- /dev/null +++ b/challenge-091/tyler-wardhaugh/lua/ch-2.lua @@ -0,0 +1,35 @@ +#!/usr/bin/env lua + +local t2 = {} +t2.DEFAULT_INPUT = {1, 2, 1, 2} + +function t2.jump_game(coll) + local i,j = 1 + while true do + if i == #coll then return true end + + j = i + coll[i] + if i == j or j > #coll then + return false + end + + i = j + end +end + +function t2.run(args) + local coll + if #args > 0 then + coll = {} + for _, v in ipairs(args) do + table.insert(coll, tonumber(v)) + end + else + coll = t2.DEFAULT_INPUT + end + + local result = t2.jump_game(coll) + if result then print(1) else print(0) end +end + +return t2 diff --git a/challenge-091/tyler-wardhaugh/lua/run.lua b/challenge-091/tyler-wardhaugh/lua/run.lua new file mode 100755 index 0000000000..c6e7473bee --- /dev/null +++ b/challenge-091/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-091/tyler-wardhaugh/lua/test.lua b/challenge-091/tyler-wardhaugh/lua/test.lua new file mode 100755 index 0000000000..a96d8fa68e --- /dev/null +++ b/challenge-091/tyler-wardhaugh/lua/test.lua @@ -0,0 +1,20 @@ +#!/usr/bin/env lua + +require 'busted.runner'() + +describe("Task 1, Count Number", function() + local t1 = require'ch-1' + it("produces correct results for the examples", function() + assert.are.same(21321314, t1.count_numbers(1122234)) + assert.are.same(12332415, t1.count_numbers(2333445)) + assert.are.same(1112131415, t1.count_numbers(12345)) + end) +end) + +describe("Task 2, Jump Game", function() + local t2 = require'ch-2' + it("produces correct results for the examples", function() + assert.truthy(t2.jump_game{1, 2, 1, 2}) + assert.falsy(t2.jump_game{2, 1, 1, 0, 2}) + end) +end) |
