aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTyler Wardhaugh <tyler.wardhaugh@gmail.com>2023-05-20 23:28:49 -0700
committerTyler Wardhaugh <tyler.wardhaugh@gmail.com>2023-05-20 23:31:44 -0700
commit5afb4e5aa13acc773018fdbb7a694a36a6d24fb9 (patch)
tree378e2d7c2754959b892a4566a4dfe16498b98eb6
parent2c1bcc0ab979fa1d961919dc782cdcc322d7ed63 (diff)
downloadperlweeklychallenge-club-5afb4e5aa13acc773018fdbb7a694a36a6d24fb9.tar.gz
perlweeklychallenge-club-5afb4e5aa13acc773018fdbb7a694a36a6d24fb9.tar.bz2
perlweeklychallenge-club-5afb4e5aa13acc773018fdbb7a694a36a6d24fb9.zip
Ch217: implement Tasks 1 & 2 in Clojure
-rw-r--r--challenge-217/tyler-wardhaugh/clojure/README.md16
-rw-r--r--challenge-217/tyler-wardhaugh/clojure/bb.edn2
-rw-r--r--challenge-217/tyler-wardhaugh/clojure/build.clj19
-rw-r--r--challenge-217/tyler-wardhaugh/clojure/deps.edn4
-rw-r--r--challenge-217/tyler-wardhaugh/clojure/src/c217/t1.clj17
-rw-r--r--challenge-217/tyler-wardhaugh/clojure/src/c217/t2.clj18
-rw-r--r--challenge-217/tyler-wardhaugh/clojure/test/c217/t1_test.clj9
-rw-r--r--challenge-217/tyler-wardhaugh/clojure/test/c217/t2_test.clj11
8 files changed, 85 insertions, 11 deletions
diff --git a/challenge-217/tyler-wardhaugh/clojure/README.md b/challenge-217/tyler-wardhaugh/clojure/README.md
index 530c7479c2..edcc7f57c7 100644
--- a/challenge-217/tyler-wardhaugh/clojure/README.md
+++ b/challenge-217/tyler-wardhaugh/clojure/README.md
@@ -1,6 +1,6 @@
-# c216
+# c217
-The Weekly Challenge — #216 — Tyler Wardhaugh
+The Weekly Challenge — #217 — Tyler Wardhaugh
## Usage
@@ -8,21 +8,21 @@ Clojure ([installation instructions](https://clojure.org/guides/getting_started#
Run Task #1:
- $ clojure -M:t1 REG COLL
+ $ clojure -M:t1 COLL
# ... or ...
- $ bb run task-1 REG COLL
+ $ bb run task-1 COLL
# Alternatively, to run it via Babashka:
- $ bb run task-1-bb REG COLL
+ $ bb run task-1-bb COLL
Run Task #2:
- $ clojure -M:t2 WORD COLL
+ $ clojure -M:t2 COLL
# ... or ...
- $ bb run task-2 WORD COLL
+ $ bb run task-2 COLL
# Alternatively, to run it via Babashka:
- $ bb run task-2-bb WORD COLL
+ $ bb run task-2-bb COLL
Run the project's tests (which are samples from the task descriptions):
diff --git a/challenge-217/tyler-wardhaugh/clojure/bb.edn b/challenge-217/tyler-wardhaugh/clojure/bb.edn
index f62b373e9f..e9f441cb63 100644
--- a/challenge-217/tyler-wardhaugh/clojure/bb.edn
+++ b/challenge-217/tyler-wardhaugh/clojure/bb.edn
@@ -1,6 +1,6 @@
{
:paths ["src" "resources"]
- :deps {c216/c216 {:local/root "."}}
+ :deps {c217/c217 {:local/root "."}}
:tasks
{
diff --git a/challenge-217/tyler-wardhaugh/clojure/build.clj b/challenge-217/tyler-wardhaugh/clojure/build.clj
new file mode 100644
index 0000000000..18baef6c0c
--- /dev/null
+++ b/challenge-217/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.c217/c217)
+(def version "0.1.0-SNAPSHOT")
+(def main 'c217.c217)
+
+(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-217/tyler-wardhaugh/clojure/deps.edn b/challenge-217/tyler-wardhaugh/clojure/deps.edn
index 0459e1f12d..9c7a50a024 100644
--- a/challenge-217/tyler-wardhaugh/clojure/deps.edn
+++ b/challenge-217/tyler-wardhaugh/clojure/deps.edn
@@ -1,8 +1,8 @@
{:paths ["src" "resources"]
:deps {org.clojure/clojure {:mvn/version "1.11.1"}}
:aliases
- {:t1 {:main-opts ["-m" "c216.t1"]}
- :t2 {:main-opts ["-m" "c216.t2"]}
+ {:t1 {:main-opts ["-m" "c217.t1"]}
+ :t2 {:main-opts ["-m" "c217.t2"]}
:build {:deps {io.github.seancorfield/build-clj
{:git/tag "v0.8.3" :git/sha "7ac1f8d"
;; since we're building an app uberjar, we do not
diff --git a/challenge-217/tyler-wardhaugh/clojure/src/c217/t1.clj b/challenge-217/tyler-wardhaugh/clojure/src/c217/t1.clj
new file mode 100644
index 0000000000..6eb9e7e85d
--- /dev/null
+++ b/challenge-217/tyler-wardhaugh/clojure/src/c217/t1.clj
@@ -0,0 +1,17 @@
+(ns c217.t1
+ (:require
+ [clojure.edn :as edn]))
+
+(def DEFAULT-INPUT [[[3 1 2] [5 2 4] [0 1 3]]])
+
+(defn third-smallest-of-sorted-matrix
+ [coll]
+ (let [sorted (->> coll (mapcat identity) sort)]
+ (nth sorted 2)))
+
+(defn -main
+ "Run Task 1 with a given input COLL, defaulting to the first example from the
+ task description."
+ [& args]
+ (let [[coll] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)]
+ (println (third-smallest-of-sorted-matrix coll))))
diff --git a/challenge-217/tyler-wardhaugh/clojure/src/c217/t2.clj b/challenge-217/tyler-wardhaugh/clojure/src/c217/t2.clj
new file mode 100644
index 0000000000..ed72f558b2
--- /dev/null
+++ b/challenge-217/tyler-wardhaugh/clojure/src/c217/t2.clj
@@ -0,0 +1,18 @@
+(ns c217.t2
+ (:require
+ [clojure.edn :as edn]
+ [clojure.string :as str]))
+
+(def DEFAULT-INPUT [[1 23]])
+
+(defn max-number
+ [coll]
+ (let [cmp (fn [a b] (compare (str b a) (str a b)))]
+ (->> coll (sort cmp) str/join parse-long)))
+
+(defn -main
+ "Run Task 2 with a given input COLL, defaulting to the first example from the
+ task description."
+ [& args]
+ (let [[coll] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)]
+ (println (max-number coll))))
diff --git a/challenge-217/tyler-wardhaugh/clojure/test/c217/t1_test.clj b/challenge-217/tyler-wardhaugh/clojure/test/c217/t1_test.clj
new file mode 100644
index 0000000000..e95fe74789
--- /dev/null
+++ b/challenge-217/tyler-wardhaugh/clojure/test/c217/t1_test.clj
@@ -0,0 +1,9 @@
+(ns c217.t1-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [c217.t1 :refer [third-smallest-of-sorted-matrix]]))
+
+(deftest task-1
+ (testing "Task 1 produces the correct results from examples in the description"
+ (is (= 1 (third-smallest-of-sorted-matrix [[3 1 2] [5 2 4] [0 1 3]])))
+ (is (= 4 (third-smallest-of-sorted-matrix [[2 1] [4 5]])))
+ (is (= 0 (third-smallest-of-sorted-matrix [[1 0 3] [0 0 0] [1 2 1]])))))
diff --git a/challenge-217/tyler-wardhaugh/clojure/test/c217/t2_test.clj b/challenge-217/tyler-wardhaugh/clojure/test/c217/t2_test.clj
new file mode 100644
index 0000000000..705d631493
--- /dev/null
+++ b/challenge-217/tyler-wardhaugh/clojure/test/c217/t2_test.clj
@@ -0,0 +1,11 @@
+(ns c217.t2-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [c217.t2 :refer [max-number]]))
+
+(deftest task-2
+ (testing "Task 2 produces the correct results from examples in the description"
+ (is (= 231 (max-number [1 23])))
+ (is (= 3210 (max-number [10 3 2])))
+ (is (= 431210 (max-number [31 2 4 10])))
+ (is (= 542111 (max-number [5 11 4 1 2])))
+ (is (= 110 (max-number [1 10])))))