aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTyler Wardhaugh <twardhaugh@cap-rx.com>2021-09-18 21:38:13 -0700
committerTyler Wardhaugh <twardhaugh@cap-rx.com>2021-09-18 21:38:13 -0700
commite80d6aaa4d64bcd8d818840dd1d5a81fd74318a4 (patch)
treebe82d8cff135064332a15c3a4e0982330dbb3a4f
parentcc51659d7bf641f5b014cfae71dfcb3ea97ddcdb (diff)
downloadperlweeklychallenge-club-e80d6aaa4d64bcd8d818840dd1d5a81fd74318a4.tar.gz
perlweeklychallenge-club-e80d6aaa4d64bcd8d818840dd1d5a81fd74318a4.tar.bz2
perlweeklychallenge-club-e80d6aaa4d64bcd8d818840dd1d5a81fd74318a4.zip
Ch129 (Clojure): Task 1
-rw-r--r--challenge-129/tyler-wardhaugh/clojure/README.md4
-rw-r--r--challenge-129/tyler-wardhaugh/clojure/src/tw/weekly/c129/t1.clj24
-rw-r--r--challenge-129/tyler-wardhaugh/clojure/test/tw/weekly/c129_test.clj14
3 files changed, 32 insertions, 10 deletions
diff --git a/challenge-129/tyler-wardhaugh/clojure/README.md b/challenge-129/tyler-wardhaugh/clojure/README.md
index 36e0cd8bb2..4bb10cd198 100644
--- a/challenge-129/tyler-wardhaugh/clojure/README.md
+++ b/challenge-129/tyler-wardhaugh/clojure/README.md
@@ -21,9 +21,9 @@ Run the project's tests (which are samples from the task descriptions):
Run Task #1 with input
- $ clojure -M -m tw.weekly.c128.t1 M
+ $ clojure -M -m tw.weekly.c128.t1 T N
# ... or ...
- $ bb run task-1 M
+ $ bb run task-1 T N
Run Task #2 with input:
diff --git a/challenge-129/tyler-wardhaugh/clojure/src/tw/weekly/c129/t1.clj b/challenge-129/tyler-wardhaugh/clojure/src/tw/weekly/c129/t1.clj
index d6592c5e9c..c028249f1b 100644
--- a/challenge-129/tyler-wardhaugh/clojure/src/tw/weekly/c129/t1.clj
+++ b/challenge-129/tyler-wardhaugh/clojure/src/tw/weekly/c129/t1.clj
@@ -1,14 +1,26 @@
(ns tw.weekly.c129.t1
- (:require [clojure.edn :as edn]))
+ (:require [clojure.edn :as edn]
+ [clojure.zip :as zip]))
;;;
; Task description for TASK #1 › Root Distance
;;;
-(def DEFAULT-INPUT [])
+(def DEFAULT-INPUT ['(1 (2) (3 nil (4 (5) (6)))) 6])
+
+(defn root-distance
+ [tree node]
+ (->> (zip/seq-zip tree)
+ (iterate zip/next)
+ (take-while (complement zip/end?))
+ (drop-while #(not= node (zip/node %)))
+ first
+ zip/path
+ count
+ dec))
(defn -main
- "Run Task 1 with a given input T, defaulting to the first example from the
- task description."
+ "Run Task 1 with a given input T (tree) and N (node), defaulting to the first
+ example from the task description."
[& args]
- (let [[T] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)]
- ))
+ (let [[T N] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)]
+ (println (root-distance T N))))
diff --git a/challenge-129/tyler-wardhaugh/clojure/test/tw/weekly/c129_test.clj b/challenge-129/tyler-wardhaugh/clojure/test/tw/weekly/c129_test.clj
index bd60de5fd7..dbef13c1ec 100644
--- a/challenge-129/tyler-wardhaugh/clojure/test/tw/weekly/c129_test.clj
+++ b/challenge-129/tyler-wardhaugh/clojure/test/tw/weekly/c129_test.clj
@@ -1,10 +1,20 @@
(ns tw.weekly.c129-test
(:require [clojure.test :refer [deftest is testing]]
- #_[tw.weekly.c129.t1 :refer []]
+ [tw.weekly.c129.t1 :refer [root-distance]]
#_[tw.weekly.c129.t2 :refer []]))
+(def tree-1 '(1 (2) (3 nil (4 (5) (6)))))
+(def tree-2 '(1 (2 (4 nil (6 (8) (9)))) (3 nil (5 (7)))))
+
(deftest task-1
- (testing "Task 1, Root Distance"))
+ (testing "Task 1, Root Distance"
+ (is (= 3 (root-distance tree-1 6)))
+ (is (= 3 (root-distance tree-1 5)))
+ (is (= 1 (root-distance tree-1 2)))
+ (is (= 2 (root-distance tree-1 4)))
+ (is (= 3 (root-distance tree-2 7)))
+ (is (= 4 (root-distance tree-2 8)))
+ (is (= 3 (root-distance tree-2 6)))))
(deftest task-2
(testing "Task 2, Add Linked Lists"))