diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-05-30 21:31:38 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-30 21:31:38 +0100 |
| commit | ae69d7cc14911e57743012c0c4f3f7cc89dcbf36 (patch) | |
| tree | 97fa382321d99aa8207d90a9ad2d0ee9314faaf9 /challenge-114 | |
| parent | d341b400063df8eea09a68167de5cf3ce672c963 (diff) | |
| parent | 4f23aa44a1f39a68384598348a69283a30036bdc (diff) | |
| download | perlweeklychallenge-club-ae69d7cc14911e57743012c0c4f3f7cc89dcbf36.tar.gz perlweeklychallenge-club-ae69d7cc14911e57743012c0c4f3f7cc89dcbf36.tar.bz2 perlweeklychallenge-club-ae69d7cc14911e57743012c0c4f3f7cc89dcbf36.zip | |
Merge pull request #4166 from tylerw/tw/challenge-114
Ch114 (Clojure): Tasks 1 & 2
Diffstat (limited to 'challenge-114')
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))))) |
