diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-05-16 18:45:05 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-16 18:45:05 +0100 |
| commit | 9a58753f25dda814dbf383f5f14a8bd4f5749f20 (patch) | |
| tree | 093bef206eb62bfb3fe845e35580d5f9722a8341 /challenge-112 | |
| parent | 17322b0b44469a6a2facd1fcbb16ff777b76ee5e (diff) | |
| parent | 4242990df21e617a25900faaa792adc548088d33 (diff) | |
| download | perlweeklychallenge-club-9a58753f25dda814dbf383f5f14a8bd4f5749f20.tar.gz perlweeklychallenge-club-9a58753f25dda814dbf383f5f14a8bd4f5749f20.tar.bz2 perlweeklychallenge-club-9a58753f25dda814dbf383f5f14a8bd4f5749f20.zip | |
Merge pull request #4085 from tylerw/tw/challenge-112
Ch112: Tasks 1 & 2
Diffstat (limited to 'challenge-112')
6 files changed, 77 insertions, 7 deletions
diff --git a/challenge-112/tyler-wardhaugh/clojure/README.md b/challenge-112/tyler-wardhaugh/clojure/README.md index 4ad284eaa5..441683043e 100644 --- a/challenge-112/tyler-wardhaugh/clojure/README.md +++ b/challenge-112/tyler-wardhaugh/clojure/README.md @@ -1,13 +1,13 @@ -# tw.weekly.c106 +# tw.weekly.c112 -The Weekly Challenge - #106 - Tyler Wardhaugh +The Weekly Challenge - #112 - Tyler Wardhaugh ## Usage Run the project directly (shows default output from both tasks): - $ clojure -M -m tw.weekly.c106.core + $ clojure -M -m tw.weekly.c112.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.c106.t1 N + $ clojure -M -m tw.weekly.c112.t1 PATH Run Task #2 with input: - $ clojure -M -m tw.weekly.c106.t2 N D + $ clojure -M -m tw.weekly.c112.t2 N ## Project Template diff --git a/challenge-112/tyler-wardhaugh/clojure/deps.edn b/challenge-112/tyler-wardhaugh/clojure/deps.edn index ab21fb11e4..ce910ebc97 100644 --- a/challenge-112/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-112/tyler-wardhaugh/clojure/deps.edn @@ -10,5 +10,5 @@ :main-opts ["-m" "cognitect.test-runner" "-d" "test"]} :uberjar {:extra-deps {seancorfield/depstar {:mvn/version "1.0.99"}} - :main-opts ["-m" "hf.depstar.uberjar" "tw.weekly.c108.jar" - "-C" "-m" "tw.weekly.c108"]}}} + :main-opts ["-m" "hf.depstar.uberjar" "tw.weekly.c112.jar" + "-C" "-m" "tw.weekly.c112"]}}} diff --git a/challenge-112/tyler-wardhaugh/clojure/src/tw/weekly/c112/core.clj b/challenge-112/tyler-wardhaugh/clojure/src/tw/weekly/c112/core.clj new file mode 100644 index 0000000000..fa5f922536 --- /dev/null +++ b/challenge-112/tyler-wardhaugh/clojure/src/tw/weekly/c112/core.clj @@ -0,0 +1,12 @@ +(ns tw.weekly.c112.core + (:require [tw.weekly.c112.t1 :as t1]) + (:require [tw.weekly.c112.t2 :as t2]) + (:gen-class)) + +(defn -main + "Run all tasks" + [& _] + (println "Task #1:") + (t1/-main) + (println "\nTask #2:") + (t2/-main)) diff --git a/challenge-112/tyler-wardhaugh/clojure/src/tw/weekly/c112/t1.clj b/challenge-112/tyler-wardhaugh/clojure/src/tw/weekly/c112/t1.clj new file mode 100644 index 0000000000..0d0d3e7354 --- /dev/null +++ b/challenge-112/tyler-wardhaugh/clojure/src/tw/weekly/c112/t1.clj @@ -0,0 +1,20 @@ +(ns tw.weekly.c112.t1 + (:require [clojure.edn :as edn]) + (:import (java.nio.file Path))) + +;;; +; Task description for TASK #1 › Canonical Path +;;; +(def DEFAULT-INPUT "/a/") + +(defn canonicalize + "Convert an absolute path to a simplified canonical one" + [path] + (let [p (Path/of path (into-array String ""))] + (-> p .normalize str))) + +(defn -main + "Run Task 1." + [& args] + (let [path (or (some-> args first edn/read-string) DEFAULT-INPUT)] + (println (canonicalize path)))) diff --git a/challenge-112/tyler-wardhaugh/clojure/src/tw/weekly/c112/t2.clj b/challenge-112/tyler-wardhaugh/clojure/src/tw/weekly/c112/t2.clj new file mode 100644 index 0000000000..90c950ed7f --- /dev/null +++ b/challenge-112/tyler-wardhaugh/clojure/src/tw/weekly/c112/t2.clj @@ -0,0 +1,23 @@ +(ns tw.weekly.c112.t2 + (:require [clojure.edn :as edn])) + +;;; +; Task description for TASK #2 › Climb Stairs +;;; +(def DEFAULT-INPUT 3) + +(def fib-seq + (map first (iterate (fn [[a b]] [b (+ a b)]) [0 1]))) + +(defn climb-stairs + "Find the distinct ways to climb n stairs, taking either 1 or 2 steps at a + time." + [n] + (->> n inc (nth fib-seq))) + +(defn -main + "Run Task 2 with a given input N, defaulting to the first example from the + task description." + [& args] + (let [N (or (some-> args first edn/read-string) DEFAULT-INPUT)] + (println (climb-stairs N)))) diff --git a/challenge-112/tyler-wardhaugh/clojure/test/tw/weekly/c112_test.clj b/challenge-112/tyler-wardhaugh/clojure/test/tw/weekly/c112_test.clj new file mode 100644 index 0000000000..a0b981237f --- /dev/null +++ b/challenge-112/tyler-wardhaugh/clojure/test/tw/weekly/c112_test.clj @@ -0,0 +1,15 @@ +(ns tw.weekly.c112-test + (:require [clojure.test :refer [deftest is testing]] + [tw.weekly.c112.t1 :refer [canonicalize]] + [tw.weekly.c112.t2 :refer [climb-stairs]])) + +(deftest task-1 + (testing "Task 1, Canonical Path" + (is (= "/a" (canonicalize "/a/"))) + (is (= "/a/b/c" (canonicalize "/a/b//c/"))) + (is (= "/a" (canonicalize "/a/b/c/../.."))))) + +(deftest task-2 + (testing "Task 2, Climb Stairs" + (is (= 3 (climb-stairs 3))) + (is (= 5 (climb-stairs 4))))) |
