From 9c5c661261f28a042c133a21e677cd7cfc20ad9a Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Sat, 5 Sep 2020 22:50:05 -0700 Subject: README: demonstrate running tasks with inputs --- challenge-076/tyler-wardhaugh/clojure/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 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 -- cgit From a9da878f06258fecae46dcbcea8394a3bcb20b82 Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Sat, 5 Sep 2020 22:37:48 -0700 Subject: 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. --- .../tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj | 27 +++++++++++----------- .../clojure/test/tw/weekly/c76_test.clj | 8 +++++-- 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" -- cgit From 1e02eff032f31ec7198c2447b4f19a6507496ca7 Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Sat, 5 Sep 2020 22:50:40 -0700 Subject: Task #2: fix command-line argument handling --- challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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)] -- cgit