aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTyler Wardhaugh <tyler.wardhaugh@entercom.com>2020-09-05 22:37:48 -0700
committerTyler Wardhaugh <tyler.wardhaugh@entercom.com>2020-09-05 23:00:25 -0700
commita9da878f06258fecae46dcbcea8394a3bcb20b82 (patch)
treea28c55d36b2d2b26e3895c1bd5d0ceb77d44eb10
parent9c5c661261f28a042c133a21e677cd7cfc20ad9a (diff)
downloadperlweeklychallenge-club-a9da878f06258fecae46dcbcea8394a3bcb20b82.tar.gz
perlweeklychallenge-club-a9da878f06258fecae46dcbcea8394a3bcb20b82.tar.bz2
perlweeklychallenge-club-a9da878f06258fecae46dcbcea8394a3bcb20b82.zip
Task #1: add more tests and fix the bugs uncovered
There were two problems uncovered by the tests, which include one test that requires using a prime twice (6 = 3 + 3), one that is a prime (11), and one that requires 3 terms (51 = 43 + 5 + 3 or 51 = 47 +2 + 2). In find-min-primes-to-sum, I had the arguments to (conj) in the wrong order for the case of requiring 3 terms. In find-min-primes-to-sum-brute-force, I was not checking possibilities involving the same primes twice, which I fixed by replacing (combo/partitions) with (combo/selections) for both 2 and 3 terms.
-rw-r--r--challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj27
-rw-r--r--challenge-076/tyler-wardhaugh/clojure/test/tw/weekly/c76_test.clj8
2 files changed, 20 insertions, 15 deletions
diff --git a/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj b/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj
index 300b89c7ff..7e6fb06990 100644
--- a/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj
+++ b/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj
@@ -7,14 +7,15 @@
(defn find-min-primes-to-sum-brute-force
"Find the minimum number of prime numbers required, whose summation gives you the argument provided, by brute force."
[n]
- (if (p/prime? n) [n]
- (as-> n x
- (p/take-below x)
- (combo/partitions x :min 2 :max 3)
- (mapcat identity x)
- (filter #(= n (reduce + %)) x)
- (sort-by count x)
- (first x))))
+ (if (p/prime? n)
+ [n]
+ (->> n
+ p/take-below
+ ((juxt #(combo/selections % 2) #(combo/selections % 3)))
+ (apply concat)
+ (filter #(= n (reduce + %)))
+ (sort-by count)
+ first)))
; basis for algorithm:
; https://stackoverflow.com/a/35756072
@@ -26,12 +27,12 @@
(p/prime? n) [n]
(even? n) (let [f (juxt #(- n %) identity)] (->> (p/primes) (filter #(p/prime? (- n %))) first f))
(and (odd? n) (p/prime? (- n 2))) [2 (- n 2)]
- :else (conj 3 (find-min-primes-to-sum (- n 3)))))
+ :else (conj (find-min-primes-to-sum (- n 3)) 3)))
(defn -main
"Run Task 1 with a number N, defaulting to the number given in the task example, 9."
[& args]
- (let [N (or (some-> args first edn/read-string) 9)]
- (let [solution (find-min-primes-to-sum N)]
- (printf "%d prime number(s) are the minimum number required to sum to %d.\n" (count solution) N)
- (println solution))))
+ (let [N (or (some-> args first edn/read-string) 9)
+ solution (find-min-primes-to-sum N)]
+ (printf "%d prime number(s) are the minimum number required to sum to %d:\n" (count solution) N)
+ (printf "%d = %s" N (apply str (interpose " + " solution)))))
diff --git a/challenge-076/tyler-wardhaugh/clojure/test/tw/weekly/c76_test.clj b/challenge-076/tyler-wardhaugh/clojure/test/tw/weekly/c76_test.clj
index 003ce81591..d6f01cb433 100644
--- a/challenge-076/tyler-wardhaugh/clojure/test/tw/weekly/c76_test.clj
+++ b/challenge-076/tyler-wardhaugh/clojure/test/tw/weekly/c76_test.clj
@@ -6,8 +6,12 @@
(deftest ch-1
(testing "Task 1"
- (is (= 2 (count (find-min-primes-to-sum 9)) (count (find-min-primes-to-sum-brute-force 9))))
- (is (= 2 (count (find-min-primes-to-sum 34)) (count (find-min-primes-to-sum-brute-force 34))))))
+ (let [n-and-results [[6 2] [9 2] [11 1] [12 2] [51 3]]
+ fns (juxt second (comp count find-min-primes-to-sum first) (comp count find-min-primes-to-sum-brute-force first))
+ results (->> n-and-results
+ (map fns)
+ (map #(is (apply = %))))]
+ (dorun results))))
(deftest ch-2
(testing "Task 2"