From 3ff04e2945a99a6750d833a0d764a36d181a933b Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Mon, 19 Oct 2020 23:10:26 -0700 Subject: Ch83 (Clojure): Task 2 --- challenge-083/tyler-wardhaugh/clojure/deps.edn | 4 +- .../clojure/src/tw/weekly/c83/core.clj | 5 +- .../clojure/src/tw/weekly/c83/t2.clj | 55 ++++++++++++++++++++++ .../clojure/test/tw/weekly/c83_test.clj | 8 +++- 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/t2.clj diff --git a/challenge-083/tyler-wardhaugh/clojure/deps.edn b/challenge-083/tyler-wardhaugh/clojure/deps.edn index 4d1263a667..e3e3cdbca6 100644 --- a/challenge-083/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-083/tyler-wardhaugh/clojure/deps.edn @@ -1,5 +1,7 @@ {: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"} + net.cgrand/xforms {:mvn/version "0.19.2"}} :aliases {:test {:extra-paths ["test"] :extra-deps {org.clojure/test.check {:mvn/version "1.0.0"}}} 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 index 03b7eadc2c..d25ddca2de 100644 --- a/challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/core.clj +++ b/challenge-083/tyler-wardhaugh/clojure/src/tw/weekly/c83/core.clj @@ -1,9 +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)) + (t1/-main) + (println "Task #2") + (t2/-main)) 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 index 870b3e6164..ff8aafb9bb 100644 --- a/challenge-083/tyler-wardhaugh/clojure/test/tw/weekly/c83_test.clj +++ b/challenge-083/tyler-wardhaugh/clojure/test/tw/weekly/c83_test.clj @@ -1,9 +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.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)))) -- cgit