aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTyler Wardhaugh <tyler.wardhaugh@gmail.com>2020-09-29 13:11:49 -0700
committerTyler Wardhaugh <tyler.wardhaugh@gmail.com>2020-09-29 13:15:51 -0700
commit0ecd59b7ec571bbdf86f7b6add1ed82750bc853f (patch)
treeff8814ff54cd26cd2b1f939ce42c67980b97fd61
parent93e474b336455bc3068af2108bc9762fe5dc08d7 (diff)
downloadperlweeklychallenge-club-0ecd59b7ec571bbdf86f7b6add1ed82750bc853f.tar.gz
perlweeklychallenge-club-0ecd59b7ec571bbdf86f7b6add1ed82750bc853f.tar.bz2
perlweeklychallenge-club-0ecd59b7ec571bbdf86f7b6add1ed82750bc853f.zip
Ch80/Task 1: handle sequences without gaps
The smallest positive integer for the sequence [1 2 3 4] is 5 (i.e., 1 + the maximum). Add a test to check for this.
-rw-r--r--challenge-080/tyler-wardhaugh/clojure/src/tw/weekly/c80/t1.clj15
-rw-r--r--challenge-080/tyler-wardhaugh/clojure/test/tw/weekly/c80_test.clj2
2 files changed, 8 insertions, 9 deletions
diff --git a/challenge-080/tyler-wardhaugh/clojure/src/tw/weekly/c80/t1.clj b/challenge-080/tyler-wardhaugh/clojure/src/tw/weekly/c80/t1.clj
index 993eb06292..ed1cc0d665 100644
--- a/challenge-080/tyler-wardhaugh/clojure/src/tw/weekly/c80/t1.clj
+++ b/challenge-080/tyler-wardhaugh/clojure/src/tw/weekly/c80/t1.clj
@@ -13,7 +13,7 @@
"Determine the smallest positive integer missing in a sequence (by set difference)."
[coll]
(if-let [missing (->> coll
- ((juxt #(set (range 1 (inc (apply max %)))) set))
+ ((juxt #(set (range 1 (+ 2 (apply max %)))) set))
(apply set/difference)
seq)]
(apply min missing)
@@ -23,10 +23,11 @@
"Determine the smallest positive integer missing in a sequence (by sorting)."
[coll]
(if-let [sorted (->> coll (filter pos-int?) sort seq)]
- (->> (map vector (iterate inc 1) sorted)
- (filter (partial apply not=))
- (take 1)
- ffirst)
+ (or (->> (map vector (iterate inc 1) sorted)
+ (filter (partial apply not=))
+ (take 1)
+ ffirst)
+ (inc (last sorted)))
1))
(def smallest-missing smallest-missing-by-sorting)
@@ -35,6 +36,4 @@
"Run Task 1 with a list of integers N, defaulting to the first one given in the examples."
[& args]
(let [N (or (some->> args (map edn/read-string)) [5 2 -2 0]) ]
- (if-let [smallest (smallest-missing N)]
- (println smallest)
- (println "No positive integer is missing in the given sequence."))))
+ (println (smallest-missing N))))
diff --git a/challenge-080/tyler-wardhaugh/clojure/test/tw/weekly/c80_test.clj b/challenge-080/tyler-wardhaugh/clojure/test/tw/weekly/c80_test.clj
index bd5520ea19..7c88e71f6b 100644
--- a/challenge-080/tyler-wardhaugh/clojure/test/tw/weekly/c80_test.clj
+++ b/challenge-080/tyler-wardhaugh/clojure/test/tw/weekly/c80_test.clj
@@ -10,7 +10,7 @@
(is (= (smallest-missing [2 0 -1]) 1))
(is (= (smallest-missing [-5 -4 -3]) 1))
(is (= (smallest-missing [1 2 3 (int 1e7)]) 4))
- (is (nil? (smallest-missing [1 4 2 3])))))
+ (is (= (smallest-missing [1 4 2 3]) 5))))
(deftest task-2
(testing "Task 2, Count Candies"