diff options
| author | Tyler Wardhaugh <twardhaugh@cap-rx.com> | 2021-04-13 17:11:21 -0700 |
|---|---|---|
| committer | Tyler Wardhaugh <twardhaugh@cap-rx.com> | 2021-04-13 17:15:43 -0700 |
| commit | c47a7657b242cb4233bba9fca1ebed461475268c (patch) | |
| tree | 591f38921b31b3ee445709123b0cabf99dea445c | |
| parent | f3bf9e7ddbab83a6725915f676f9bf9de35bc01f (diff) | |
| download | perlweeklychallenge-club-c47a7657b242cb4233bba9fca1ebed461475268c.tar.gz perlweeklychallenge-club-c47a7657b242cb4233bba9fca1ebed461475268c.tar.bz2 perlweeklychallenge-club-c47a7657b242cb4233bba9fca1ebed461475268c.zip | |
Ch108 (Clojure): Tasks 1 & 2
5 files changed, 72 insertions, 2 deletions
diff --git a/challenge-108/tyler-wardhaugh/clojure/deps.edn b/challenge-108/tyler-wardhaugh/clojure/deps.edn index a781a94ba4..ab21fb11e4 100644 --- a/challenge-108/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-108/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.c102.jar" - "-C" "-m" "tw.weekly.c102"]}}} + :main-opts ["-m" "hf.depstar.uberjar" "tw.weekly.c108.jar" + "-C" "-m" "tw.weekly.c108"]}}} diff --git a/challenge-108/tyler-wardhaugh/clojure/src/tw/weekly/c108/core.clj b/challenge-108/tyler-wardhaugh/clojure/src/tw/weekly/c108/core.clj new file mode 100644 index 0000000000..fbdfb2fc07 --- /dev/null +++ b/challenge-108/tyler-wardhaugh/clojure/src/tw/weekly/c108/core.clj @@ -0,0 +1,12 @@ +(ns tw.weekly.c108.core + (:require [tw.weekly.c108.t1 :as t1]) + (:require [tw.weekly.c108.t2 :as t2]) + (:gen-class)) + +(defn -main + "Run all tasks" + [& _] + (println "Task #1:") + (t1/-main) + (println "\nTask #2:") + (t2/-main)) diff --git a/challenge-108/tyler-wardhaugh/clojure/src/tw/weekly/c108/t1.clj b/challenge-108/tyler-wardhaugh/clojure/src/tw/weekly/c108/t1.clj new file mode 100644 index 0000000000..60c7f2feeb --- /dev/null +++ b/challenge-108/tyler-wardhaugh/clojure/src/tw/weekly/c108/t1.clj @@ -0,0 +1,14 @@ +(ns tw.weekly.c108.t1 + (:require [clojure.pprint :refer [cl-format]])) + +;;; +; Task description for TASK #1 › Locate Memory +;;; + +(def myvar 0) + +(defn -main + "Run Task 1." + [& _] + (let [hashcode (System/identityHashCode myvar)] + (cl-format true "~a: value=~a hashcode=~a~%" #'myvar myvar hashcode))) diff --git a/challenge-108/tyler-wardhaugh/clojure/src/tw/weekly/c108/t2.clj b/challenge-108/tyler-wardhaugh/clojure/src/tw/weekly/c108/t2.clj new file mode 100644 index 0000000000..5f6795096f --- /dev/null +++ b/challenge-108/tyler-wardhaugh/clojure/src/tw/weekly/c108/t2.clj @@ -0,0 +1,37 @@ +(ns tw.weekly.c108.t2 + (:require [clojure.edn :as edn] + [clojure.pprint :refer [cl-format]])) + +;;; +; Task description for TASK #2 › Bell Numbers +;;; +(def DEFAULT-INPUT 10) + +;;; +; translation of: +; (Haskell) +; a011971 n k = a011971_tabl !! n !! k +; a011971_row n = a011971_tabl !! n +; a011971_tabl = iterate (\row -> scanl (+) (last row) row) [1] +; source: +; https://oeis.org/A011971 +; Aitken's array: triangle of numbers {a(n,k), n >= 0, 0 <= k <= n} read by +; rows, defined by a(0,0)=1, a(n,0) = a(n-1,n-1), a(n,k) = a(n,k-1) + +; a(n-1,k-1). +; linked from: +; https://oeis.org/A000110 +; Bell or exponential numbers: number of ways to partition a set of n +; labeled elements. +;;; +(defn bell-numbers + "Return a list of the first n Bell Numbers" + [n] + (let [f (fn [row] (reductions + (last row) row)) + table (concat [[1]] (take (dec n) (iterate f [1])))] + (sequence (map last) table))) + +(defn -main + "Run Task 2 with a given CLASSNAME, defaulting to a standard Java class" + [& args] + (let [N (or (some-> args first edn/read-string) DEFAULT-INPUT)] + (cl-format true "~{~a~^, ~}~%" (bell-numbers N)))) diff --git a/challenge-108/tyler-wardhaugh/clojure/test/tw/weekly/c108_test.clj b/challenge-108/tyler-wardhaugh/clojure/test/tw/weekly/c108_test.clj new file mode 100644 index 0000000000..c91d5e687e --- /dev/null +++ b/challenge-108/tyler-wardhaugh/clojure/test/tw/weekly/c108_test.clj @@ -0,0 +1,7 @@ +(ns tw.weekly.c108-test + (:require [clojure.test :refer [deftest is testing]] + [tw.weekly.c108.t2 :refer [bell-numbers]])) + +(deftest task-2 + (testing "Task 2 Bell Numbers" + (is (= [1 1 2 5 15 52 203 877 4140 21147] (bell-numbers 10))))) |
