diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-09-06 07:05:03 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-06 07:05:03 +0100 |
| commit | 068ee427accf30baa143387058b79f66b5d9faa6 (patch) | |
| tree | 1258be287aaa32bd4e7b519ef4691d73fe501bbb | |
| parent | a2c6ebc43ebba2b09e07b70f5a55705e9bf70bd1 (diff) | |
| parent | 1e02eff032f31ec7198c2447b4f19a6507496ca7 (diff) | |
| download | perlweeklychallenge-club-068ee427accf30baa143387058b79f66b5d9faa6.tar.gz perlweeklychallenge-club-068ee427accf30baa143387058b79f66b5d9faa6.tar.bz2 perlweeklychallenge-club-068ee427accf30baa143387058b79f66b5d9faa6.zip | |
Merge pull request #2214 from tylerw/tw/challenge-076
Bug fixes for Challenge #076
4 files changed, 24 insertions, 20 deletions
diff --git a/challenge-076/tyler-wardhaugh/clojure/README.md b/challenge-076/tyler-wardhaugh/clojure/README.md index a46a7a9fc9..e29e9b74d6 100644 --- a/challenge-076/tyler-wardhaugh/clojure/README.md +++ b/challenge-076/tyler-wardhaugh/clojure/README.md @@ -15,11 +15,11 @@ Run the project's tests (which are samples from the task descriptions): Run Task #1 with input - $ clojure -m tw.weekly.ch-1 + $ clojure -m tw.weekly.ch-1 N Run Task #2 with input: - $ clojure -m tw.weekly.ch-2 + $ clojure -m tw.weekly.ch-2 GRID_FILE DICT_FILE ## Project Template 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/src/tw/weekly/ch_2.clj b/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj index cd5109aab1..4b4e2e7279 100644 --- a/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj +++ b/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj @@ -1,5 +1,4 @@ (ns tw.weekly.ch-2 - (:require [clojure.edn :as edn]) (:require [clojure.java.io :as io]) (:require [clojure.string :as str]) (:require [clojure.core.matrix :as mat])) @@ -50,8 +49,8 @@ (defn -main "Run Task 2 with a grid file and a dictionary file, defaulting to the ones in the resources directory." [& args] - (let [grid-file (or (some-> args first edn/read-string) (io/resource "grid.txt")) - dict-file (or (some-> args second edn/read-string) (io/resource "dict.txt")) + (let [grid-file (or (some-> args first io/file) (io/resource "grid.txt")) + dict-file (or (some-> args second io/file) (io/resource "dict.txt")) grid (parse-grid-file grid-file) dict (parse-dict-file dict-file) words (word-search grid dict)] 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" |
