aboutsummaryrefslogtreecommitdiff
path: root/challenge-083/tyler-wardhaugh
diff options
context:
space:
mode:
authorSteve Rogerson <steve.git@yewtc.demon.co.uk>2020-10-23 12:27:13 +0100
committerSteve Rogerson <steve.git@yewtc.demon.co.uk>2020-10-23 12:27:13 +0100
commit44c2bbf87ac613a2a442cc4e54d810c20a042ff2 (patch)
tree3af39af1f773a9e6b44e91a3852a7553dae7c986 /challenge-083/tyler-wardhaugh
parentff3c07c3e4409c8d507b3e69496c690b58de524d (diff)
parent89421f14095148aefcd254da3d728b6150b22cc3 (diff)
downloadperlweeklychallenge-club-44c2bbf87ac613a2a442cc4e54d810c20a042ff2.tar.gz
perlweeklychallenge-club-44c2bbf87ac613a2a442cc4e54d810c20a042ff2.tar.bz2
perlweeklychallenge-club-44c2bbf87ac613a2a442cc4e54d810c20a042ff2.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-083/tyler-wardhaugh')
-rw-r--r--challenge-083/tyler-wardhaugh/clojure/README.md8
-rw-r--r--challenge-083/tyler-wardhaugh/clojure/deps.edn7
-rw-r--r--challenge-083/tyler-wardhaugh/clojure/pom.xml14
-rw-r--r--challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/core.clj12
-rw-r--r--challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/t1.clj32
-rw-r--r--challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/t2.clj55
-rw-r--r--challenge-083/tyler-wardhaugh/clojure/test/tw/weekly/c83_test.clj15
-rw-r--r--challenge-083/tyler-wardhaugh/lua/README.md6
-rwxr-xr-xchallenge-083/tyler-wardhaugh/lua/ch-1.lua20
-rwxr-xr-xchallenge-083/tyler-wardhaugh/lua/ch-2.lua34
-rwxr-xr-xchallenge-083/tyler-wardhaugh/lua/run.lua9
-rwxr-xr-xchallenge-083/tyler-wardhaugh/lua/test.lua20
12 files changed, 215 insertions, 17 deletions
diff --git a/challenge-083/tyler-wardhaugh/clojure/README.md b/challenge-083/tyler-wardhaugh/clojure/README.md
index 546cb8db65..bf37bd62cc 100644
--- a/challenge-083/tyler-wardhaugh/clojure/README.md
+++ b/challenge-083/tyler-wardhaugh/clojure/README.md
@@ -1,4 +1,4 @@
-# tw.weekly.c82
+# tw.weekly.c83
The Weekly Challenge - #082 - Tyler Wardhaugh
@@ -7,7 +7,7 @@ The Weekly Challenge - #082 - Tyler Wardhaugh
Run the project directly (shows default output from both tasks):
- $ clojure -M -m tw.weekly.c82.core
+ $ clojure -M -m tw.weekly.c83.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.c82.t1 M N
+ $ clojure -M -m tw.weekly.c83.t1 S
Run Task #2 with input:
- $ clojure -M -m tw.weekly.c82.t2 S1 S2
+ $ clojure -M -m tw.weekly.c83.t2 A1 A2 A3...
## Project Template
diff --git a/challenge-083/tyler-wardhaugh/clojure/deps.edn b/challenge-083/tyler-wardhaugh/clojure/deps.edn
index c692c74c45..e3e3cdbca6 100644
--- a/challenge-083/tyler-wardhaugh/clojure/deps.edn
+++ b/challenge-083/tyler-wardhaugh/clojure/deps.edn
@@ -1,6 +1,7 @@
{:paths ["src" "resources"]
:deps {org.clojure/clojure {:mvn/version "1.10.1"}
- org.clojure/math.numeric-tower {:mvn/version "0.0.4"}}
+ org.clojure/math.combinatorics {:mvn/version "0.1.6"}
+ net.cgrand/xforms {:mvn/version "0.19.2"}}
: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.c82.jar"
- "-C" "-m" "tw.weekly.c82"]}}}
+ :main-opts ["-m" "hf.depstar.uberjar" "tw.weekly.c83.jar"
+ "-C" "-m" "tw.weekly.c83"]}}}
diff --git a/challenge-083/tyler-wardhaugh/clojure/pom.xml b/challenge-083/tyler-wardhaugh/clojure/pom.xml
index 92eb55d64a..7d5a4bb861 100644
--- a/challenge-083/tyler-wardhaugh/clojure/pom.xml
+++ b/challenge-083/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.c82</artifactId>
+ <artifactId>tw.weekly.c83</artifactId>
<version>0.1.0-SNAPSHOT</version>
- <name>tw.weekly.c82</name>
- <description>Challenge #082</description>
- <url>https://github.com/tw.weekly/tw.weekly.c82</url>
+ <name>tw.weekly.c83</name>
+ <description>Challenge #083</description>
+ <url>https://github.com/tw.weekly/tw.weekly.c83</url>
<licenses>
<license>
<name>Eclipse Public License</name>
@@ -19,9 +19,9 @@
</developer>
</developers>
<scm>
- <url>https://github.com/tw.weekly/tw.weekly.c82</url>
- <connection>scm:git:git://github.com/tw.weekly/tw.weekly.c82.git</connection>
- <developerConnection>scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c82.git</developerConnection>
+ <url>https://github.com/tw.weekly/tw.weekly.c83</url>
+ <connection>scm:git:git://github.com/tw.weekly/tw.weekly.c83.git</connection>
+ <developerConnection>scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c83.git</developerConnection>
<tag>HEAD</tag>
</scm>
<dependencies>
diff --git a/challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/core.clj b/challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/core.clj
new file mode 100644
index 0000000000..d25ddca2de
--- /dev/null
+++ b/challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/core.clj
@@ -0,0 +1,12 @@
+(ns tw.weekly.c83.core
+ (:require [tw.weekly.c83.t1 :as t1])
+ (:require [tw.weekly.c83.t2 :as t2])
+ (:gen-class))
+
+(defn -main
+ "Run all tasks"
+ [& _]
+ (println "Task #1")
+ (t1/-main)
+ (println "Task #2")
+ (t2/-main))
diff --git a/challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/t1.clj b/challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/t1.clj
new file mode 100644
index 0000000000..36ca66aec0
--- /dev/null
+++ b/challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/t1.clj
@@ -0,0 +1,32 @@
+(ns tw.weekly.c83.t1
+ (:require [clojure.string :as str]))
+
+;;; Task description for TASK #1 › Words Length
+; Submitted by: Mohammad S Anwar
+; You are given a string $S with 3 or more words.
+;
+; Write a script to find the length of the string except the first and last words ignoring whitespace.
+;
+; Example 1:
+; Input: $S = "The Weekly Challenge"
+; Output: 6
+;
+;
+; Example 2:
+; Input: $S = "The purpose of our lives is to be happy"
+; Output: 23
+;
+;;;
+
+(defn inner-words-length
+ "Return the combined length of the inner words, ignoring whitespace."
+ [s]
+ (let [source (->> (str/split s #" ") (drop 1) (drop-last 1))]
+ (transduce (map count) + source)))
+
+(defn -main
+ "Run Task 1 with a strings S, defaulting to the first example given in the task description."
+ [& args]
+ (let [S (or (some-> args first) "The Weekly Challenge")
+ len (inner-words-length S)]
+ (println len)))
diff --git a/challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/t2.clj b/challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/t2.clj
new file mode 100644
index 0000000000..1dc22c8372
--- /dev/null
+++ b/challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/t2.clj
@@ -0,0 +1,55 @@
+(ns tw.weekly.c83.t2
+ (:require [clojure.edn :as edn])
+ (:require [clojure.math.combinatorics :as combo])
+ (:require [net.cgrand.xforms :as x]))
+
+;;; Task description for
+; TASK #2 › Flip Array
+; Submitted by: Mohammad S Anwar
+; You are given an array @A of positive numbers.
+;
+; Write a script to flip the sign of some members of the given array so that
+; the sum of the all members is minimum non-negative.
+;
+; Given an array of positive elements, you have to flip the sign of some of its
+; elements such that the resultant sum of the elements of array should be
+; minimum non-negative(as close to zero as possible). Return the minimum no. of
+; elements whose sign needs to be flipped such that the resultant sum is
+; minimum non-negative.
+;
+; Example 1:
+; Input: @A = (3, 10, 8)
+; Output: 1
+
+; Explanation:
+; Flipping the sign of just one element 10 gives the result 1 i.e. (3) + (-10) + (8) = 1
+;
+;
+; Example 2:
+; Input: @A = (12, 2, 10)
+; Output: 1
+;
+; Explanation:
+; Flipping the sign of just one element 12 gives the result 0 i.e. (-12) + (2) + (10) = 0
+;
+;;;
+
+(defn flip-array
+ "Determine the minimum number of 'flips' needed to produce the smallest non-negative sum."
+ [coll]
+ (let [source (->> coll (map (juxt - +)) (apply combo/cartesian-product))
+ xf (comp
+ (x/by-key (partial reduce +) (x/into []))
+ (remove (comp neg? first))
+ x/min
+ x/vals
+ cat
+ (map (comp count (partial filter neg?))))]
+ (reduce min ##Inf (eduction xf source))))
+
+(defn -main
+ "Run Task 2 with an array of integers, defaulting to the example given in the task description."
+ [& args]
+ (let [A (or (some->> args (map edn/read-string)) [3 10 8])
+ minimum (flip-array A)]
+ (println minimum)))
diff --git a/challenge-083/tyler-wardhaugh/clojure/test/tw/weekly/c83_test.clj b/challenge-083/tyler-wardhaugh/clojure/test/tw/weekly/c83_test.clj
new file mode 100644
index 0000000000..ff8aafb9bb
--- /dev/null
+++ b/challenge-083/tyler-wardhaugh/clojure/test/tw/weekly/c83_test.clj
@@ -0,0 +1,15 @@
+(ns tw.weekly.c83-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [tw.weekly.c83.t1 :refer [inner-words-length]]
+ [tw.weekly.c83.t2 :refer [flip-array]]))
+
+(deftest task-1
+ (testing "Task 1, Words Length"
+ (is (= (inner-words-length "The Weekly Challenge") 6))
+ (is (= (inner-words-length "The purpose of our lives is to be happy") 23))
+ (is (= (inner-words-length "Zero when-no-inner-words-exist!") 0))))
+
+(deftest task-2
+ (testing "Task 2, Flip Array"
+ (is (= (flip-array [3 10 8]) 1))
+ (is (= (flip-array [12 2 10]) 1))))
diff --git a/challenge-083/tyler-wardhaugh/lua/README.md b/challenge-083/tyler-wardhaugh/lua/README.md
index 8247d79487..35e8606521 100644
--- a/challenge-083/tyler-wardhaugh/lua/README.md
+++ b/challenge-083/tyler-wardhaugh/lua/README.md
@@ -1,17 +1,17 @@
# The Weekly Challenge
-The Weekly Challenge - #081 - Tyler Wardhaugh
+The Weekly Challenge - #083 - Tyler Wardhaugh
## Usage
Run Task 1:
- $ ./run.lua ch-1 M N
+ $ ./run.lua ch-1 S
Run Task 2:
- $ ./run.lua ch-2 A B C
+ $ ./run.lua ch-2 A1 A2 A3...
Run the project's tests (all the samples from the task descriptions plus some others):
diff --git a/challenge-083/tyler-wardhaugh/lua/ch-1.lua b/challenge-083/tyler-wardhaugh/lua/ch-1.lua
new file mode 100755
index 0000000000..cb4f35d915
--- /dev/null
+++ b/challenge-083/tyler-wardhaugh/lua/ch-1.lua
@@ -0,0 +1,20 @@
+#!/usr/bin/env lua
+
+local t1 = {}
+
+function t1.inner_words_length(s)
+ local inner = s:match("^%S+%s+%f[%S](.*)%f[%s]%s+%S+$")
+ if inner then
+ return inner:gsub("%s+", ""):len()
+ else
+ return 0
+ end
+end
+
+function t1.run(args)
+ local s = args[1]
+ local len = t1.inner_words_length(s)
+ print(len)
+end
+
+return t1
diff --git a/challenge-083/tyler-wardhaugh/lua/ch-2.lua b/challenge-083/tyler-wardhaugh/lua/ch-2.lua
new file mode 100755
index 0000000000..bf900a864e
--- /dev/null
+++ b/challenge-083/tyler-wardhaugh/lua/ch-2.lua
@@ -0,0 +1,34 @@
+#!/usr/bin/env lua
+
+local t2 = {}
+
+function t2.flip_array(coll)
+ local min_flips = math.maxinteger
+ local min_sum = math.maxinteger
+ local max_bits = 2^#coll - 1
+
+ local cur_num, cur_sum, is_neg
+ for bits = 1, max_bits do
+ cur_num, cur_sum, is_neg = 0, 0, 0
+ for i, v in ipairs(coll) do
+ is_neg = bits & 2^(i-1) == 0
+ cur_num = cur_num + (is_neg and 1 or 0)
+ cur_sum = cur_sum + v * (is_neg and -1 or 1)
+ end
+
+ if 0 <= cur_sum and cur_sum <= min_sum then
+ min_flips = math.min(cur_num, min_flips)
+ min_sum = cur_sum
+ end
+ end
+
+ return min_flips
+end
+
+
+function t2.run(args)
+ local minimum = t2.flip_array(args)
+ print(minimum)
+end
+
+return t2
diff --git a/challenge-083/tyler-wardhaugh/lua/run.lua b/challenge-083/tyler-wardhaugh/lua/run.lua
new file mode 100755
index 0000000000..c6e7473bee
--- /dev/null
+++ b/challenge-083/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-083/tyler-wardhaugh/lua/test.lua b/challenge-083/tyler-wardhaugh/lua/test.lua
new file mode 100755
index 0000000000..7fa7effa5f
--- /dev/null
+++ b/challenge-083/tyler-wardhaugh/lua/test.lua
@@ -0,0 +1,20 @@
+#!/usr/bin/env lua
+
+require 'busted.runner'()
+
+describe("Task 1, Words Length", function()
+ local t1 = require'ch-1'
+ it("produces correct results for the examples", function()
+ assert.are.same(t1.inner_words_length"The Weekly Challenge", 6)
+ assert.are.same(t1.inner_words_length"The purpose of our lives is to be happy", 23)
+ assert.are.same(t1.inner_words_length"Zero when-no-inner-words-exist!", 0)
+ end)
+end)
+
+describe("Task 2, Flip Array", function()
+ local t2 = require'ch-2'
+ it("produces correct results for the examples", function()
+ assert.are.same(t2.flip_array({3, 10, 8}), 1)
+ assert.are.same(t2.flip_array({12, 2, 10}), 1)
+ end)
+end)