diff options
4 files changed, 68 insertions, 0 deletions
diff --git a/challenge-114/tyler-wardhaugh/clojure/src/tw/weekly/c114/core.clj b/challenge-114/tyler-wardhaugh/clojure/src/tw/weekly/c114/core.clj new file mode 100644 index 0000000000..a4bfa04373 --- /dev/null +++ b/challenge-114/tyler-wardhaugh/clojure/src/tw/weekly/c114/core.clj @@ -0,0 +1,12 @@ +(ns tw.weekly.c114.core + (:require [tw.weekly.c114.t1 :as t1]) + (:require [tw.weekly.c114.t2 :as t2]) + (:gen-class)) + +(defn -main + "Run all tasks" + [& _] + (println "Task #1:") + (t1/-main) + (println "\nTask #2:") + (t2/-main)) diff --git a/challenge-114/tyler-wardhaugh/clojure/src/tw/weekly/c114/t1.clj b/challenge-114/tyler-wardhaugh/clojure/src/tw/weekly/c114/t1.clj new file mode 100644 index 0000000000..61d56a3d6a --- /dev/null +++ b/challenge-114/tyler-wardhaugh/clojure/src/tw/weekly/c114/t1.clj @@ -0,0 +1,21 @@ +(ns tw.weekly.c114.t1 + (:require [clojure.edn :as edn] + [clojure.string :as str])) + +;;; +; Task description for TASK #1 › Next Palindrome Number +;;; +(def DEFAULT-INPUT 1234) + +(defn next-palindrome + [n] + (let [source (->> n inc (iterate inc)) + xf (filter #(apply = ((juxt identity str/reverse) (str %))))] + (->> source (sequence xf) first))) + +(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 first edn/read-string) DEFAULT-INPUT)] + (println (next-palindrome N)))) diff --git a/challenge-114/tyler-wardhaugh/clojure/src/tw/weekly/c114/t2.clj b/challenge-114/tyler-wardhaugh/clojure/src/tw/weekly/c114/t2.clj new file mode 100644 index 0000000000..67c3a17118 --- /dev/null +++ b/challenge-114/tyler-wardhaugh/clojure/src/tw/weekly/c114/t2.clj @@ -0,0 +1,21 @@ +(ns tw.weekly.c114.t2 + (:require [clojure.edn :as edn])) + +;;; +; Task description for TASK #2 › Higher Integer Set Bits +;;; +(def DEFAULT-INPUT 3) + +(defn higher-int-set-bits + [n] + (let [source (->> n inc (iterate inc)) + ones (Integer/bitCount n) + xf (filter #(= ones (Integer/bitCount %)))] + (->> source (sequence xf) first))) + +(defn -main + "Run Task 2 with a given input N, defaulting to the first example from the + task description." + [& args] + (let [N (or (some-> args first edn/read-string) DEFAULT-INPUT)] + (println (higher-int-set-bits N)))) diff --git a/challenge-114/tyler-wardhaugh/clojure/test/tw/weekly/c114_test.clj b/challenge-114/tyler-wardhaugh/clojure/test/tw/weekly/c114_test.clj new file mode 100644 index 0000000000..0f4822a6b3 --- /dev/null +++ b/challenge-114/tyler-wardhaugh/clojure/test/tw/weekly/c114_test.clj @@ -0,0 +1,14 @@ +(ns tw.weekly.c114-test + (:require [clojure.test :refer [deftest is testing]] + [tw.weekly.c114.t1 :refer [next-palindrome]] + [tw.weekly.c114.t2 :refer [higher-int-set-bits]])) + +(deftest task-1 + (testing "Task 1, Next Palindrome Number" + (is (= 1331 (next-palindrome 1234))) + (is (= 1001 (next-palindrome 999))))) + +(deftest task-2 + (testing "Task 2, Higher Integer Set Bits" + (is (= 5 (higher-int-set-bits 3))) + (is (= 17 (higher-int-set-bits 12))))) |
