aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-146/tyler-wardhaugh/clojure/src/c146/t2.clj22
-rw-r--r--challenge-146/tyler-wardhaugh/clojure/test/c146/t2_test.clj11
2 files changed, 33 insertions, 0 deletions
diff --git a/challenge-146/tyler-wardhaugh/clojure/src/c146/t2.clj b/challenge-146/tyler-wardhaugh/clojure/src/c146/t2.clj
new file mode 100644
index 0000000000..b259c8edc8
--- /dev/null
+++ b/challenge-146/tyler-wardhaugh/clojure/src/c146/t2.clj
@@ -0,0 +1,22 @@
+(ns c146.t2
+ (:require [clojure.edn :as edn]
+ [clojure.pprint :refer [cl-format]]))
+
+; Clojure supports Ratios (including as literals)
+(def DEFAULT-INPUT [3/5])
+
+(defn parent
+ [x]
+ (when (not= x 1)
+ (let [[n d] ((juxt numerator denominator) x)]
+ (if (< x 1)
+ (/ n (- d n)) ; left
+ (/ (- n d) d))))) ; right
+
+(defn -main
+ "Run Task 2 with a given input M, defaulting to the first example from the
+ task description."
+ [& args]
+ (let [[M] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)]
+ (cl-format true "~{parent = '~a' and grandparent = '~a'~}~%"
+ (->> M (iterate parent) (drop 1) (take 2)))))
diff --git a/challenge-146/tyler-wardhaugh/clojure/test/c146/t2_test.clj b/challenge-146/tyler-wardhaugh/clojure/test/c146/t2_test.clj
new file mode 100644
index 0000000000..48dd9ca00a
--- /dev/null
+++ b/challenge-146/tyler-wardhaugh/clojure/test/c146/t2_test.clj
@@ -0,0 +1,11 @@
+(ns c146.t2-test
+ (:refer-clojure :exclude [ancestors])
+ (:require [clojure.test :refer [deftest is testing]]
+ [c146.t2 :refer [parent]]))
+
+(def ancestors (juxt parent (comp parent parent)))
+
+(deftest examples
+ (testing "Examples from task description"
+ (is (= [3/2 1/2] (ancestors 3/5)))
+ (is (= [1/3 1/2] (ancestors 4/3)))))