aboutsummaryrefslogtreecommitdiff
path: root/challenge-083/tyler-wardhaugh/clojure
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-083/tyler-wardhaugh/clojure')
-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
7 files changed, 129 insertions, 14 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))))