aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTyler Wardhaugh <twardhaugh@cap-rx.com>2021-10-06 07:51:00 -0700
committerTyler Wardhaugh <twardhaugh@cap-rx.com>2021-10-06 07:51:00 -0700
commitc138ad6152c0ee9f2a456b586e5ea73605566c46 (patch)
tree0292b814ae2f3c9d4527293f23a98e26937cdf00
parentb74a46400a288a71d511f5ef412ff2dd86a91d95 (diff)
downloadperlweeklychallenge-club-c138ad6152c0ee9f2a456b586e5ea73605566c46.tar.gz
perlweeklychallenge-club-c138ad6152c0ee9f2a456b586e5ea73605566c46.tar.bz2
perlweeklychallenge-club-c138ad6152c0ee9f2a456b586e5ea73605566c46.zip
Ch133 (Clojure): Task 1
-rw-r--r--challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/t1.clj19
-rw-r--r--challenge-133/tyler-wardhaugh/clojure/test/tw/weekly/c133/t1_test.clj11
2 files changed, 29 insertions, 1 deletions
diff --git a/challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/t1.clj b/challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/t1.clj
index 64c77e4ed7..52c713ec7e 100644
--- a/challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/t1.clj
+++ b/challenge-133/tyler-wardhaugh/clojure/src/tw/weekly/c133/t1.clj
@@ -6,9 +6,26 @@
;;;
(def DEFAULT-INPUT [10])
+(defn isqrt-easy
+ "Return the integer square root of n, the easy way."
+ [n]
+ (int (Math/sqrt n)))
+
+(defn isqrt
+ "Return the integer square root of n, a slightly more manual way."
+ [n]
+ (let [x (quot n 2)
+ next-x (fn [x] (quot (+ x (quot n x)) 2))
+ xf (comp
+ (partition-all 2)
+ (drop-while (partial apply not=)))]
+ (->> (iterate next-x x)
+ (sequence xf)
+ ffirst)))
+
(defn -main
"Run Task 1 with a given input N, defaulting to the first example from the
task description."
[& args]
(let [[N] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)]
- ))
+ (println (isqrt N))))
diff --git a/challenge-133/tyler-wardhaugh/clojure/test/tw/weekly/c133/t1_test.clj b/challenge-133/tyler-wardhaugh/clojure/test/tw/weekly/c133/t1_test.clj
new file mode 100644
index 0000000000..126ea39d18
--- /dev/null
+++ b/challenge-133/tyler-wardhaugh/clojure/test/tw/weekly/c133/t1_test.clj
@@ -0,0 +1,11 @@
+(ns tw.weekly.c133.t1-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [tw.weekly.c133.t1 :refer [isqrt isqrt-easy]]))
+
+(deftest examples
+ (testing "Examples from description"
+ (let [both (juxt isqrt isqrt-easy)]
+ (is (apply = 3 (both 10)))
+ (is (apply = 5 (both 27)))
+ (is (apply = 9 (both 85)))
+ (is (apply = 10 (both 101))))))