aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-03-19 23:30:43 +0000
committerGitHub <noreply@github.com>2022-03-19 23:30:43 +0000
commit1dfaba440c9b08178baba37bedeb440093026c31 (patch)
treeca64f1d1207ffe54b645f0cf4f68f0fc34b0181f
parentcb656c351b80d28d0b06d1d192084380c02000a3 (diff)
parent9c286415fd8e21d6d7cbf3e2fa357d72fff9b632 (diff)
downloadperlweeklychallenge-club-1dfaba440c9b08178baba37bedeb440093026c31.tar.gz
perlweeklychallenge-club-1dfaba440c9b08178baba37bedeb440093026c31.tar.bz2
perlweeklychallenge-club-1dfaba440c9b08178baba37bedeb440093026c31.zip
Merge pull request #5796 from tylerw/tw/challenge-156
Challenge 156
-rw-r--r--challenge-156/tyler-wardhaugh/clojure/README.md12
-rw-r--r--challenge-156/tyler-wardhaugh/clojure/bb.edn4
-rw-r--r--challenge-156/tyler-wardhaugh/clojure/build.clj19
-rw-r--r--challenge-156/tyler-wardhaugh/clojure/deps.edn4
-rw-r--r--challenge-156/tyler-wardhaugh/clojure/src/c156/t1.clj12
-rw-r--r--challenge-156/tyler-wardhaugh/clojure/src/c156/t2.clj25
-rw-r--r--challenge-156/tyler-wardhaugh/clojure/test/c156/t1_test.clj10
-rw-r--r--challenge-156/tyler-wardhaugh/clojure/test/c156/t2_test.clj8
8 files changed, 84 insertions, 10 deletions
diff --git a/challenge-156/tyler-wardhaugh/clojure/README.md b/challenge-156/tyler-wardhaugh/clojure/README.md
index 0172d72218..6ccda4438f 100644
--- a/challenge-156/tyler-wardhaugh/clojure/README.md
+++ b/challenge-156/tyler-wardhaugh/clojure/README.md
@@ -1,6 +1,6 @@
# c154
-The Weekly Challenge — #154 — Tyler Wardhaugh
+The Weekly Challenge — #156 — Tyler Wardhaugh
## Usage
@@ -12,14 +12,14 @@ Run Task #1:
# ... or ...
$ bb run task-1
+ # Alternatively, to run it via Babashka:
+ $ bb run task-1-bb
+
Run Task #2:
- $ clojure -M:t2
+ $ clojure -M:t2 N
# ... or ...
- $ bb run task-2
-
- # Alternatively, to run it via Babashka:
- $ bb run task-2-bb
+ $ bb run task-2 N
Run the project's tests (which are samples from the task descriptions):
diff --git a/challenge-156/tyler-wardhaugh/clojure/bb.edn b/challenge-156/tyler-wardhaugh/clojure/bb.edn
index 6d0b679197..8e5e3b49c6 100644
--- a/challenge-156/tyler-wardhaugh/clojure/bb.edn
+++ b/challenge-156/tyler-wardhaugh/clojure/bb.edn
@@ -72,12 +72,12 @@
:task (run-task-clj :t1 *command-line-args*)}
task-1-bb {:doc "Run Task 1 (via Babashka)"
- :task (bb-no-go :t1 *command-line-args*)}
+ :task (run-task-bb :t1 *command-line-args*)}
task-2 {:doc "Run Task 2 (via clojure)"
:task (run-task-clj :t2 *command-line-args*)}
task-2-bb {:doc "Run Task 2 (via Babashka)"
- :task (run-task-bb :t2 *command-line-args*)}
+ :task (bb-no-go :t2 *command-line-args*)}
}
}
diff --git a/challenge-156/tyler-wardhaugh/clojure/build.clj b/challenge-156/tyler-wardhaugh/clojure/build.clj
new file mode 100644
index 0000000000..937053b000
--- /dev/null
+++ b/challenge-156/tyler-wardhaugh/clojure/build.clj
@@ -0,0 +1,19 @@
+(ns build
+ (:refer-clojure :exclude [test])
+ (:require [org.corfield.build :as bb]))
+
+(def lib 'net.clojars.c156/c156)
+(def version "0.1.0-SNAPSHOT")
+(def main 'c156.c156)
+
+(defn test "Run the tests." [opts]
+ (bb/run-tests opts))
+
+(def clean bb/clean)
+
+(defn ci "Run the CI pipeline of tests (and build the uberjar)." [opts]
+ (-> opts
+ (assoc :lib lib :version version :main main)
+ (bb/run-tests)
+ (bb/clean)
+ (bb/uber)))
diff --git a/challenge-156/tyler-wardhaugh/clojure/deps.edn b/challenge-156/tyler-wardhaugh/clojure/deps.edn
index d647404dd7..138cff091a 100644
--- a/challenge-156/tyler-wardhaugh/clojure/deps.edn
+++ b/challenge-156/tyler-wardhaugh/clojure/deps.edn
@@ -2,8 +2,8 @@
:deps {org.clojure/clojure {:mvn/version "1.11.0-rc1"}
org.clojure/math.combinatorics {:mvn/version "0.1.6"}}
:aliases
- {:t1 {:main-opts ["-m" "c154.t1"]}
- :t2 {:main-opts ["-m" "c154.t2"]}
+ {:t1 {:main-opts ["-m" "c156.t1"]}
+ :t2 {:main-opts ["-m" "c156.t2"]}
:build {:deps {io.github.seancorfield/build-clj
{:git/tag "v0.6.3" :git/sha "9b8e09b"
;; since we're building an app uberjar, we do not
diff --git a/challenge-156/tyler-wardhaugh/clojure/src/c156/t1.clj b/challenge-156/tyler-wardhaugh/clojure/src/c156/t1.clj
new file mode 100644
index 0000000000..de60ed1c90
--- /dev/null
+++ b/challenge-156/tyler-wardhaugh/clojure/src/c156/t1.clj
@@ -0,0 +1,12 @@
+(ns c156.t1
+ (:require [clojure.pprint :refer [cl-format]]))
+
+(defn pernicious?
+ [n]
+ (let [ones (BigInteger/valueOf (.bitCount (BigInteger/valueOf n)))]
+ (.isProbablePrime ones 1000)))
+
+(defn -main
+ "Run Task 1."
+ [& args]
+ (cl-format true "~{~a~^, ~}~%" (->> (range) (filter pernicious?) (take 10))))
diff --git a/challenge-156/tyler-wardhaugh/clojure/src/c156/t2.clj b/challenge-156/tyler-wardhaugh/clojure/src/c156/t2.clj
new file mode 100644
index 0000000000..1e295bf40d
--- /dev/null
+++ b/challenge-156/tyler-wardhaugh/clojure/src/c156/t2.clj
@@ -0,0 +1,25 @@
+(ns c156.t2
+ (:require [clojure.edn :as edn]
+ [clojure.math.combinatorics :as combo]))
+
+(def DEFAULT-INPUT [12])
+
+(defn proper-divisors
+ [n]
+ (->> (range 2 n) (filter #(zero? (rem n %)))))
+
+(defn weird?
+ [n]
+ (let [divs (proper-divisors n)]
+ (and
+ (> (reduce + divs) n)
+ (not-any?
+ #{n}
+ (->> (combo/subsets divs) (map #(reduce + %)))))))
+
+(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 (map edn/read-string)) DEFAULT-INPUT)]
+ (println (if (weird? N) 1 0))))
diff --git a/challenge-156/tyler-wardhaugh/clojure/test/c156/t1_test.clj b/challenge-156/tyler-wardhaugh/clojure/test/c156/t1_test.clj
new file mode 100644
index 0000000000..c74bd3fa40
--- /dev/null
+++ b/challenge-156/tyler-wardhaugh/clojure/test/c156/t1_test.clj
@@ -0,0 +1,10 @@
+(ns c156.t1-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [c156.t1 :refer [pernicious?]]))
+
+; Expected Output, from https://theweeklychallenge.org/blog/perl-weekly-challenge-156/#TASK1
+(def EXPECTED [3, 5, 6, 7, 9, 10, 11, 12, 13, 14])
+
+(deftest task-1
+ (testing "Task 1 produces the correct result"
+ (is (= EXPECTED (->> (range) (filter pernicious?) (take 10))))))
diff --git a/challenge-156/tyler-wardhaugh/clojure/test/c156/t2_test.clj b/challenge-156/tyler-wardhaugh/clojure/test/c156/t2_test.clj
new file mode 100644
index 0000000000..14c3bb455f
--- /dev/null
+++ b/challenge-156/tyler-wardhaugh/clojure/test/c156/t2_test.clj
@@ -0,0 +1,8 @@
+(ns c156.t2-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [c156.t2 :refer [weird?]]))
+
+(deftest task-2
+ (testing "Task 2 produces the correct result"
+ (is (false? (weird? 12)))
+ (is (true? (weird? 70)))))