diff options
6 files changed, 106 insertions, 7 deletions
diff --git a/challenge-098/tyler-wardhaugh/clojure/README.md b/challenge-098/tyler-wardhaugh/clojure/README.md index 0e314a09c0..dbeaac833c 100644 --- a/challenge-098/tyler-wardhaugh/clojure/README.md +++ b/challenge-098/tyler-wardhaugh/clojure/README.md @@ -1,13 +1,13 @@ -# tw.weekly.c96 +# tw.weekly.c98 -The Weekly Challenge - #096 - Tyler Wardhaugh +The Weekly Challenge - #098 - Tyler Wardhaugh ## Usage Run the project directly (shows default output from both tasks): - $ clojure -M -m tw.weekly.c96.core + $ clojure -M -m tw.weekly.c98.core Run the project's tests (which are samples from the task descriptions): @@ -15,11 +15,11 @@ Run the project's tests (which are samples from the task descriptions): Run Task #1 with input - $ clojure -M -m tw.weekly.c96.t1 S + $ clojure -M -m tw.weekly.c98.t1 FILE N -Run Task #2: +Run Task #2 with input: - $ clojure -M -m tw.weekly.c96.t2 S1 S2 + $ clojure -M -m tw.weekly.c98.t2 NS N ## Project Template @@ -29,7 +29,7 @@ See [seancorfield/clj-new: Generate new projects based on clj, Boot, or Leininge ## License -Copyright © 2020 Tyler Wardhaugh +Copyright © 2021 Tyler Wardhaugh Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version. diff --git a/challenge-098/tyler-wardhaugh/clojure/resources/input.txt b/challenge-098/tyler-wardhaugh/clojure/resources/input.txt new file mode 100644 index 0000000000..6a537b5b36 --- /dev/null +++ b/challenge-098/tyler-wardhaugh/clojure/resources/input.txt @@ -0,0 +1 @@ +1234567890
\ No newline at end of file diff --git a/challenge-098/tyler-wardhaugh/clojure/src/tw/weekly/c98/core.clj b/challenge-098/tyler-wardhaugh/clojure/src/tw/weekly/c98/core.clj new file mode 100644 index 0000000000..8186db73ab --- /dev/null +++ b/challenge-098/tyler-wardhaugh/clojure/src/tw/weekly/c98/core.clj @@ -0,0 +1,12 @@ +(ns tw.weekly.c98.core + (:require [tw.weekly.c98.t1 :as t1]) + (:require [tw.weekly.c98.t2 :as t2]) + (:gen-class)) + +(defn -main + "Run all tasks" + [& _] + (println "Task #1:") + (t1/-main) + (println "\nTask #2:") + (t2/-main)) diff --git a/challenge-098/tyler-wardhaugh/clojure/src/tw/weekly/c98/t1.clj b/challenge-098/tyler-wardhaugh/clojure/src/tw/weekly/c98/t1.clj new file mode 100644 index 0000000000..e3ea20d117 --- /dev/null +++ b/challenge-098/tyler-wardhaugh/clojure/src/tw/weekly/c98/t1.clj @@ -0,0 +1,43 @@ +(ns tw.weekly.c98.t1 + (:import (java.io RandomAccessFile)) + (:require [clojure.edn :as edn] + [clojure.java.io :as io])) + +;;; +; Task description for TASK #1 › Read N-characters +;;; + +(def DEFAULT-INPUT [(-> "input.txt" io/resource io/file) 4]) +(def FILES (atom {})) + +(defn readN + "read n characters from file and move the pointer to the (n+1)th character" + [file n] + (let [rdr (get @FILES file (RandomAccessFile. file "r")) + bytes (byte-array n) + result (.read rdr bytes)] + (when (pos? result) + (swap! FILES assoc file rdr) + (->> bytes + (take-while pos?) + (map char) + (apply str))))) + +(defn add-shutdown-hook! + "Add a shutdown hook to ensure we close all our file readers." + [] + (.addShutdownHook + (Runtime/getRuntime) + (Thread. (fn [] + (doseq [[_ rdr] @FILES] + (.close rdr)))))) + +(defn -main + "Run Task 1 using a file F and size S, defaulting to the example + given in the task description. Call readN on these values three times + and print the result." + [& args] + (let [F (or (some-> args first io/file) (first DEFAULT-INPUT)) + S (or (some-> args second edn/read-string) (second DEFAULT-INPUT))] + (add-shutdown-hook!) + (dotimes [_ 3] (println (readN F S))))) diff --git a/challenge-098/tyler-wardhaugh/clojure/src/tw/weekly/c98/t2.clj b/challenge-098/tyler-wardhaugh/clojure/src/tw/weekly/c98/t2.clj new file mode 100644 index 0000000000..9c3fbd3f73 --- /dev/null +++ b/challenge-098/tyler-wardhaugh/clojure/src/tw/weekly/c98/t2.clj @@ -0,0 +1,25 @@ +(ns tw.weekly.c98.t2 + (:import java.util.Arrays) + (:require [clojure.edn :as edn])) + +;;; +; Task description for TASK #2 › Search Insert Position +;;; + +(def DEFAULT-INPUT [[1 2 3 4] 3]) + +(defn search-insert-position + "Find the search insert position for n into a sorted sequence of integers + coll" + [coll n] + (let [index (Arrays/binarySearch (int-array coll) n)] + (if (neg? index) + (-> index - dec) + index))) + +(defn -main + "Run Task 2 using a sorted sequence of integers NS and a target N, + defaulting to the example given in the task description." + [& args] + (let [[NS N] (or (some->> args (take 2) (map edn/read-string)) DEFAULT-INPUT)] + (println (search-insert-position NS N)))) diff --git a/challenge-098/tyler-wardhaugh/clojure/test/tw/weekly/c98_test.clj b/challenge-098/tyler-wardhaugh/clojure/test/tw/weekly/c98_test.clj new file mode 100644 index 0000000000..7bbfca2203 --- /dev/null +++ b/challenge-098/tyler-wardhaugh/clojure/test/tw/weekly/c98_test.clj @@ -0,0 +1,18 @@ +(ns tw.weekly.c98-test + (:require [clojure.test :refer [deftest is testing]] + [tw.weekly.c98.t1 :refer [readN] :as t1] + [tw.weekly.c98.t2 :refer [search-insert-position]])) + +(deftest task-1 + (testing "Task 1, Caesar Cipher" + (t1/add-shutdown-hook!) + (is (= "1234" (apply readN t1/DEFAULT-INPUT))) + (is (= "5678" (apply readN t1/DEFAULT-INPUT))) + (is (= "90" (apply readN t1/DEFAULT-INPUT))))) + +(deftest task-2 + (testing "Task 2, Search Insert Position" + (is (= 2 (search-insert-position [1 2 3 4] 3))) + (is (= 3 (search-insert-position [1 3 5 7] 6))) + (is (zero? (search-insert-position [12 14 16 18] 10))) + (is (= 4 (search-insert-position [11 13 15 17] 19))))) |
