aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTyler Wardhaugh <tyler.wardhaugh@gmail.com>2022-12-31 16:35:46 -0800
committerTyler Wardhaugh <tyler.wardhaugh@gmail.com>2022-12-31 17:23:58 -0800
commita1dd0328f2a16c06afa4c887c38bd5d287e3e319 (patch)
tree8e150338a9ef87f8ae84a4bf9ddda982c5595e49
parent63fb76188e132564e50feefd2d9d5b8491568948 (diff)
downloadperlweeklychallenge-club-a1dd0328f2a16c06afa4c887c38bd5d287e3e319.tar.gz
perlweeklychallenge-club-a1dd0328f2a16c06afa4c887c38bd5d287e3e319.tar.bz2
perlweeklychallenge-club-a1dd0328f2a16c06afa4c887c38bd5d287e3e319.zip
Ch197: implement Tasks 1 & 2 in Clojure
-rw-r--r--challenge-197/tyler-wardhaugh/clojure/README.md10
-rw-r--r--challenge-197/tyler-wardhaugh/clojure/bb.edn2
-rw-r--r--challenge-197/tyler-wardhaugh/clojure/build.clj19
-rw-r--r--challenge-197/tyler-wardhaugh/clojure/deps.edn4
-rw-r--r--challenge-197/tyler-wardhaugh/clojure/src/c197/t1.clj18
-rw-r--r--challenge-197/tyler-wardhaugh/clojure/src/c197/t2.clj20
-rw-r--r--challenge-197/tyler-wardhaugh/clojure/test/c197/t1_test.clj9
-rw-r--r--challenge-197/tyler-wardhaugh/clojure/test/c197/t2_test.clj8
8 files changed, 82 insertions, 8 deletions
diff --git a/challenge-197/tyler-wardhaugh/clojure/README.md b/challenge-197/tyler-wardhaugh/clojure/README.md
index 063cccb9e8..478f7395fd 100644
--- a/challenge-197/tyler-wardhaugh/clojure/README.md
+++ b/challenge-197/tyler-wardhaugh/clojure/README.md
@@ -1,6 +1,6 @@
-# c193
+# c197
-The Weekly Challenge — #193 — Tyler Wardhaugh
+The Weekly Challenge — #197 — Tyler Wardhaugh
## Usage
@@ -17,12 +17,12 @@ Run Task #1:
Run Task #2:
- $ clojure -M:t2 N
+ $ clojure -M:t2 COLL
# ... or ...
- $ bb run task-2 N
+ $ bb run task-2 COLL
# Alternatively, to run it via Babashka:
- $ bb run task-2-bb N
+ $ bb run task-2-bb COLL
Run the project's tests (which are samples from the task descriptions):
diff --git a/challenge-197/tyler-wardhaugh/clojure/bb.edn b/challenge-197/tyler-wardhaugh/clojure/bb.edn
index 885507f3e5..f02268dce7 100644
--- a/challenge-197/tyler-wardhaugh/clojure/bb.edn
+++ b/challenge-197/tyler-wardhaugh/clojure/bb.edn
@@ -1,6 +1,6 @@
{
:paths ["src" "resources"]
- :deps {c193/c193 {:local/root "."}}
+ :deps {c197/c197 {:local/root "."}}
:tasks
{
diff --git a/challenge-197/tyler-wardhaugh/clojure/build.clj b/challenge-197/tyler-wardhaugh/clojure/build.clj
new file mode 100644
index 0000000000..db7cd8411e
--- /dev/null
+++ b/challenge-197/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.c197/c197)
+(def version "0.1.0-SNAPSHOT")
+(def main 'c197.c197)
+
+(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-197/tyler-wardhaugh/clojure/deps.edn b/challenge-197/tyler-wardhaugh/clojure/deps.edn
index bda220ad94..a4f57caf90 100644
--- a/challenge-197/tyler-wardhaugh/clojure/deps.edn
+++ b/challenge-197/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" "c193.t1"]}
- :t2 {:main-opts ["-m" "c193.t2"]}
+ {:t1 {:main-opts ["-m" "c197.t1"]}
+ :t2 {:main-opts ["-m" "c197.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-197/tyler-wardhaugh/clojure/src/c197/t1.clj b/challenge-197/tyler-wardhaugh/clojure/src/c197/t1.clj
new file mode 100644
index 0000000000..da04a8dc2d
--- /dev/null
+++ b/challenge-197/tyler-wardhaugh/clojure/src/c197/t1.clj
@@ -0,0 +1,18 @@
+(ns c197.t1
+ (:require
+ [clojure.edn :as edn]
+ [clojure.pprint :refer [cl-format]]))
+
+(def DEFAULT-INPUT ['(1, 0, 3, 0, 0, 5)])
+
+(defn move-zero
+ [coll]
+ (let [{z true, nz false} (group-by zero? coll)]
+ (into nz z)))
+
+(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)]
+ (cl-format true "(~{~a~^, ~})~%" (move-zero coll))))
diff --git a/challenge-197/tyler-wardhaugh/clojure/src/c197/t2.clj b/challenge-197/tyler-wardhaugh/clojure/src/c197/t2.clj
new file mode 100644
index 0000000000..06c0263344
--- /dev/null
+++ b/challenge-197/tyler-wardhaugh/clojure/src/c197/t2.clj
@@ -0,0 +1,20 @@
+(ns c197.t2
+ (:require
+ [clojure.edn :as edn]
+ [clojure.pprint :refer [cl-format]]))
+
+(def DEFAULT-INPUT ['(1,5,1,1,6,4)])
+
+(defn wiggle-sort
+ "Tweaked wiggle sort that goes by n1 <= n2 >= n3 ..."
+ [coll]
+ (let [mid (quot (inc (count coll)) 2)
+ [high low] (split-at mid (sort (comp - compare) coll))]
+ (interleave low high)))
+
+(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)]
+ (cl-format true "(~{~a~^, ~})~%" (wiggle-sort coll))))
diff --git a/challenge-197/tyler-wardhaugh/clojure/test/c197/t1_test.clj b/challenge-197/tyler-wardhaugh/clojure/test/c197/t1_test.clj
new file mode 100644
index 0000000000..c17dd1cc7d
--- /dev/null
+++ b/challenge-197/tyler-wardhaugh/clojure/test/c197/t1_test.clj
@@ -0,0 +1,9 @@
+(ns c197.t1-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [c197.t1 :refer [move-zero]]))
+
+(deftest task-1
+ (testing "Task 1 produces the correct results (description)"
+ (is (= (move-zero [1 0 3 0 0 5]) [1 3 5 0 0 0]))
+ (is (= (move-zero [1 6 4]) [1 6 4]))
+ (is (= (move-zero [0 1 0 2 0]) [1 2 0 0 0]))))
diff --git a/challenge-197/tyler-wardhaugh/clojure/test/c197/t2_test.clj b/challenge-197/tyler-wardhaugh/clojure/test/c197/t2_test.clj
new file mode 100644
index 0000000000..e831025885
--- /dev/null
+++ b/challenge-197/tyler-wardhaugh/clojure/test/c197/t2_test.clj
@@ -0,0 +1,8 @@
+(ns c197.t2-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [c197.t2 :refer [wiggle-sort]]))
+
+(deftest task-2
+ (testing "Task 2 produces the correct results"
+ (is (= (wiggle-sort [1 5 1 1 6 4]) [1 6 1 5 1 4]))
+ (is (= (wiggle-sort [1 3 2 2 3 1]) [2 3 1 3 1 2]))))