aboutsummaryrefslogtreecommitdiff
path: root/challenge-157
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-03-27 20:36:26 +0100
committerGitHub <noreply@github.com>2022-03-27 20:36:26 +0100
commita1e8d3643037dafb58e8ec8f140f177ce1e9a7f4 (patch)
tree8d9054e8ba19de46ecd7d89b2012bf76de68abbc /challenge-157
parent0b70a3d2318be537bd795e734c1c2c52d7fc4686 (diff)
parentf2f3750976d60fd4e39c912f5521bab06e876020 (diff)
downloadperlweeklychallenge-club-a1e8d3643037dafb58e8ec8f140f177ce1e9a7f4.tar.gz
perlweeklychallenge-club-a1e8d3643037dafb58e8ec8f140f177ce1e9a7f4.tar.bz2
perlweeklychallenge-club-a1e8d3643037dafb58e8ec8f140f177ce1e9a7f4.zip
Merge pull request #5840 from tylerw/tw/challenge-157
Challenge 157
Diffstat (limited to 'challenge-157')
-rw-r--r--challenge-157/tyler-wardhaugh/clojure/README.md13
-rw-r--r--challenge-157/tyler-wardhaugh/clojure/bb.edn2
-rw-r--r--challenge-157/tyler-wardhaugh/clojure/build.clj19
-rw-r--r--challenge-157/tyler-wardhaugh/clojure/deps.edn7
-rw-r--r--challenge-157/tyler-wardhaugh/clojure/src/c157/t1.clj30
-rw-r--r--challenge-157/tyler-wardhaugh/clojure/src/c157/t2.clj27
-rw-r--r--challenge-157/tyler-wardhaugh/clojure/test/c157/t1_test.clj12
-rw-r--r--challenge-157/tyler-wardhaugh/clojure/test/c157/t2_test.clj9
8 files changed, 109 insertions, 10 deletions
diff --git a/challenge-157/tyler-wardhaugh/clojure/README.md b/challenge-157/tyler-wardhaugh/clojure/README.md
index 6ccda4438f..dd84955593 100644
--- a/challenge-157/tyler-wardhaugh/clojure/README.md
+++ b/challenge-157/tyler-wardhaugh/clojure/README.md
@@ -1,6 +1,6 @@
-# c154
+# c157
-The Weekly Challenge — #156 — Tyler Wardhaugh
+The Weekly Challenge — #157 — Tyler Wardhaugh
## Usage
@@ -8,12 +8,12 @@ Clojure ([installation instructions](https://clojure.org/guides/getting_started#
Run Task #1:
- $ clojure -M:t1
+ $ clojure -M:t1 N
# ... or ...
- $ bb run task-1
+ $ bb run task-1 N
# Alternatively, to run it via Babashka:
- $ bb run task-1-bb
+ $ bb run task-1-bb N
Run Task #2:
@@ -27,6 +27,9 @@ Run the project's tests (which are samples from the task descriptions):
# ... or ...
$ bb run test
+ # Alternatively, to run it via Babashka:
+ $ bb run task-2-bb N
+
View available tasks Babashka can run:
$ bb tasks
diff --git a/challenge-157/tyler-wardhaugh/clojure/bb.edn b/challenge-157/tyler-wardhaugh/clojure/bb.edn
index 8e5e3b49c6..e21cd63a1e 100644
--- a/challenge-157/tyler-wardhaugh/clojure/bb.edn
+++ b/challenge-157/tyler-wardhaugh/clojure/bb.edn
@@ -78,6 +78,6 @@
:task (run-task-clj :t2 *command-line-args*)}
task-2-bb {:doc "Run Task 2 (via Babashka)"
- :task (bb-no-go :t2 *command-line-args*)}
+ :task (run-task-bb :t2 *command-line-args*)}
}
}
diff --git a/challenge-157/tyler-wardhaugh/clojure/build.clj b/challenge-157/tyler-wardhaugh/clojure/build.clj
new file mode 100644
index 0000000000..4266ccd756
--- /dev/null
+++ b/challenge-157/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.c157/c157)
+(def version "0.1.0-SNAPSHOT")
+(def main 'c157.c157)
+
+(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-157/tyler-wardhaugh/clojure/deps.edn b/challenge-157/tyler-wardhaugh/clojure/deps.edn
index 138cff091a..76983f14cb 100644
--- a/challenge-157/tyler-wardhaugh/clojure/deps.edn
+++ b/challenge-157/tyler-wardhaugh/clojure/deps.edn
@@ -1,9 +1,8 @@
{:paths ["src" "resources"]
- :deps {org.clojure/clojure {:mvn/version "1.11.0-rc1"}
- org.clojure/math.combinatorics {:mvn/version "0.1.6"}}
+ :deps {org.clojure/clojure {:mvn/version "1.11.0"}}
:aliases
- {:t1 {:main-opts ["-m" "c156.t1"]}
- :t2 {:main-opts ["-m" "c156.t2"]}
+ {:t1 {:main-opts ["-m" "c157.t1"]}
+ :t2 {:main-opts ["-m" "c157.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-157/tyler-wardhaugh/clojure/src/c157/t1.clj b/challenge-157/tyler-wardhaugh/clojure/src/c157/t1.clj
new file mode 100644
index 0000000000..0557037dbb
--- /dev/null
+++ b/challenge-157/tyler-wardhaugh/clojure/src/c157/t1.clj
@@ -0,0 +1,30 @@
+(ns c157.t1
+ (:require [clojure.edn :as edn]
+ [clojure.math :as m]
+ [clojure.pprint :refer [cl-format]]))
+
+(def DEFAULT-INPUT [[1,3,5,6,9]])
+
+(defn am
+ "Returns the arithmetic mean of coll"
+ [coll]
+ (/ (reduce + coll) (count coll)))
+
+(defn gm
+ "Returns the geometric mean of coll"
+ [coll]
+ (m/pow (reduce * coll) (/ (count coll))))
+
+(defn hm
+ "Returns the harmonic mean of coll"
+ [coll]
+ (/ (count coll) (transduce (map /) + coll)))
+
+(def pythagorean-means (juxt am gm hm))
+
+(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)]
+ (cl-format true "AM = ~,1f, GM = ~,1f, HM = ~,1f" (am N) (gm N) (hm N))))
diff --git a/challenge-157/tyler-wardhaugh/clojure/src/c157/t2.clj b/challenge-157/tyler-wardhaugh/clojure/src/c157/t2.clj
new file mode 100644
index 0000000000..2fbaab9c4a
--- /dev/null
+++ b/challenge-157/tyler-wardhaugh/clojure/src/c157/t2.clj
@@ -0,0 +1,27 @@
+(ns c157.t2
+ (:require [clojure.edn :as edn]
+ [clojure.pprint :refer [cl-format]]))
+
+(def DEFAULT-INPUT [7])
+
+(defn- check-by-base
+ [n]
+ (->> (range 2 (dec n))
+ (filter
+ (fn [b]
+ (== 1 (-> n BigInteger/valueOf (.toString b) distinct count))))
+ (some identity)
+ boolean))
+
+(defn brazilian? [n]
+ (cond
+ (< n 7) false
+ (and (even? n) (>= n 8)) true
+ :else (check-by-base n)))
+
+(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 (brazilian? N) 1 0))))
diff --git a/challenge-157/tyler-wardhaugh/clojure/test/c157/t1_test.clj b/challenge-157/tyler-wardhaugh/clojure/test/c157/t1_test.clj
new file mode 100644
index 0000000000..90fb59470e
--- /dev/null
+++ b/challenge-157/tyler-wardhaugh/clojure/test/c157/t1_test.clj
@@ -0,0 +1,12 @@
+(ns c157.t1-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [clojure.pprint :refer [cl-format]]
+ [c157.t1 :refer [pythagorean-means]]))
+
+(def calc (comp (partial map #(cl-format nil "~,1f" %)) pythagorean-means))
+
+(deftest task-1
+ (testing "Task 1 produces the correct result"
+ (is (= ["4.8" "3.8" "2.8"] (calc [1,3,5,6,9])))
+ (is (= ["6.0" "5.2" "4.4"] (calc [2,4,6,8,10])))
+ (is (= ["3.0" "2.6" "2.2"] (calc [1,2,3,4,5])))))
diff --git a/challenge-157/tyler-wardhaugh/clojure/test/c157/t2_test.clj b/challenge-157/tyler-wardhaugh/clojure/test/c157/t2_test.clj
new file mode 100644
index 0000000000..04c14a4d56
--- /dev/null
+++ b/challenge-157/tyler-wardhaugh/clojure/test/c157/t2_test.clj
@@ -0,0 +1,9 @@
+(ns c157.t2-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [c157.t2 :refer [brazilian?]]))
+
+(deftest task-2
+ (testing "Task 2 produces the correct result"
+ (is (true? (brazilian? 7)))
+ (is (true? (brazilian? 6)))
+ (is (true? (brazilian? 8)))))