diff options
| author | Tyler Wardhaugh <twardhaugh@cap-rx.com> | 2021-09-18 21:38:13 -0700 |
|---|---|---|
| committer | Tyler Wardhaugh <twardhaugh@cap-rx.com> | 2021-09-18 21:43:23 -0700 |
| commit | 68ccc79d6fcde8d19d55fada2e67c972398a8a52 (patch) | |
| tree | 540b4f6c371468f023145741f1c17894363695d3 | |
| parent | e80d6aaa4d64bcd8d818840dd1d5a81fd74318a4 (diff) | |
| download | perlweeklychallenge-club-68ccc79d6fcde8d19d55fada2e67c972398a8a52.tar.gz perlweeklychallenge-club-68ccc79d6fcde8d19d55fada2e67c972398a8a52.tar.bz2 perlweeklychallenge-club-68ccc79d6fcde8d19d55fada2e67c972398a8a52.zip | |
Ch129 (Clojure): Task 2
3 files changed, 31 insertions, 10 deletions
diff --git a/challenge-129/tyler-wardhaugh/clojure/README.md b/challenge-129/tyler-wardhaugh/clojure/README.md index 4bb10cd198..3a1ae7a3bb 100644 --- a/challenge-129/tyler-wardhaugh/clojure/README.md +++ b/challenge-129/tyler-wardhaugh/clojure/README.md @@ -27,9 +27,9 @@ Run Task #1 with input Run Task #2 with input: - $ clojure -M -m tw.weekly.c128.t2 A D + $ clojure -M -m tw.weekly.c128.t2 L1 L2 # ... or ... - $ bb run task-2 A D + $ bb run task-2 L1 L2 View available tasks Babashka can run: diff --git a/challenge-129/tyler-wardhaugh/clojure/src/tw/weekly/c129/t2.clj b/challenge-129/tyler-wardhaugh/clojure/src/tw/weekly/c129/t2.clj index 39275957c2..f1aa677745 100644 --- a/challenge-129/tyler-wardhaugh/clojure/src/tw/weekly/c129/t2.clj +++ b/challenge-129/tyler-wardhaugh/clojure/src/tw/weekly/c129/t2.clj @@ -1,14 +1,33 @@ (ns tw.weekly.c129.t2 - (:require [clojure.edn :as edn])) + (:require [clojure.edn :as edn] + [clojure.pprint :refer [cl-format]])) ;;; ; Task description for TASK #2, Add Linked Lists ;;; -(def DEFAULT-INPUT []) + +; Traditionally, in Lisps the standard list is a linked list; therefore the +; core language (and standard library) contain a multitude of functions for +; dealing with them. In that vein, we'll use the built-in list for this task. +(def DEFAULT-INPUT ['(1 2 3) '(3 2 1)]) + +(defn list->num + [coll] + (->> (map * (reverse coll) (iterate #(* 10 %) 1)) + (reduce +))) + +(defn num->list + [n] + (->> n str (map #(Character/digit % 10)))) + +(defn add-lists + [& colls] + (-> (transduce (map list->num) + colls) + num->list)) (defn -main - "Run Task 1 with a given input L, defaulting to the first example from the - task description." + "Run Task 1 with a given inputs L1 and L2, defaulting to the first example + from the task description." [& args] - (let [[L] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)] - )) + (let [[L1 L2] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)] + (cl-format true "~{~a~^ -> ~}~%" (add-lists L1 L2)))) 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 dbef13c1ec..814efe0998 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,7 +1,7 @@ (ns tw.weekly.c129-test (:require [clojure.test :refer [deftest is testing]] [tw.weekly.c129.t1 :refer [root-distance]] - #_[tw.weekly.c129.t2 :refer []])) + [tw.weekly.c129.t2 :refer [add-lists]])) (def tree-1 '(1 (2) (3 nil (4 (5) (6))))) (def tree-2 '(1 (2 (4 nil (6 (8) (9)))) (3 nil (5 (7))))) @@ -17,4 +17,6 @@ (is (= 3 (root-distance tree-2 6))))) (deftest task-2 - (testing "Task 2, Add Linked Lists")) + (testing "Task 2, Add Linked Lists" + (is (= '(4 4 4) (add-lists '(1 2 3) '(3 2 1)))) + (is (= '(1 3 0 0 0) (add-lists '(1 2 3 4 5) '(6 5 5)))))) |
