aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-01-23 00:52:55 +0000
committerGitHub <noreply@github.com>2023-01-23 00:52:55 +0000
commite3e622b6d1519f744c7cffcfdad9a7a4d463fad3 (patch)
tree6193c1fa4ce38a2daa597380a977b50afd3dda60
parent3b1937c70a5bf42d1f7b274d9f37ec6bfa3866c4 (diff)
parent7c6911516bead2bedf6f67fb6b20d8ce5e6370e5 (diff)
downloadperlweeklychallenge-club-e3e622b6d1519f744c7cffcfdad9a7a4d463fad3.tar.gz
perlweeklychallenge-club-e3e622b6d1519f744c7cffcfdad9a7a4d463fad3.tar.bz2
perlweeklychallenge-club-e3e622b6d1519f744c7cffcfdad9a7a4d463fad3.zip
Merge pull request #7445 from tylerw/tw/challenge-200
Ch200: solve Task 1 in Clojure
-rw-r--r--challenge-200/tyler-wardhaugh/clojure/README.md10
-rw-r--r--challenge-200/tyler-wardhaugh/clojure/bb.edn2
-rw-r--r--challenge-200/tyler-wardhaugh/clojure/build.clj19
-rw-r--r--challenge-200/tyler-wardhaugh/clojure/deps.edn4
-rw-r--r--challenge-200/tyler-wardhaugh/clojure/src/c200/t1.clj29
-rw-r--r--challenge-200/tyler-wardhaugh/clojure/src/c200/t2.clj10
-rw-r--r--challenge-200/tyler-wardhaugh/clojure/test/c200/t1_test.clj8
-rw-r--r--challenge-200/tyler-wardhaugh/clojure/test/c200/t2_test.clj7
8 files changed, 81 insertions, 8 deletions
diff --git a/challenge-200/tyler-wardhaugh/clojure/README.md b/challenge-200/tyler-wardhaugh/clojure/README.md
index 23b26176e1..103893d9b9 100644
--- a/challenge-200/tyler-wardhaugh/clojure/README.md
+++ b/challenge-200/tyler-wardhaugh/clojure/README.md
@@ -1,6 +1,6 @@
-# c199
+# c200
-The Weekly Challenge — #199 — Tyler Wardhaugh
+The Weekly Challenge — #200 — Tyler Wardhaugh
## Usage
@@ -17,12 +17,12 @@ Run Task #1:
Run Task #2:
- $ clojure -M:t2 X Y Z COLL
+ $ clojure -M:t2 N
# ... or ...
- $ bb run task-2 COLL
+ $ bb run task-2 N
# Alternatively, to run it via Babashka:
- $ bb run task-2-bb COLL
+ $ bb run task-2-bb N
Run the project's tests (which are samples from the task descriptions):
diff --git a/challenge-200/tyler-wardhaugh/clojure/bb.edn b/challenge-200/tyler-wardhaugh/clojure/bb.edn
index 4d2688a77c..d7f9593e83 100644
--- a/challenge-200/tyler-wardhaugh/clojure/bb.edn
+++ b/challenge-200/tyler-wardhaugh/clojure/bb.edn
@@ -1,6 +1,6 @@
{
:paths ["src" "resources"]
- :deps {c199/c199 {:local/root "."}}
+ :deps {c200/c200 {:local/root "."}}
:tasks
{
diff --git a/challenge-200/tyler-wardhaugh/clojure/build.clj b/challenge-200/tyler-wardhaugh/clojure/build.clj
new file mode 100644
index 0000000000..6b9d8fcd2f
--- /dev/null
+++ b/challenge-200/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.c200/c200)
+(def version "0.1.0-SNAPSHOT")
+(def main 'c200.c200)
+
+(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-200/tyler-wardhaugh/clojure/deps.edn b/challenge-200/tyler-wardhaugh/clojure/deps.edn
index e09444c323..bcf2a4462e 100644
--- a/challenge-200/tyler-wardhaugh/clojure/deps.edn
+++ b/challenge-200/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" "c199.t1"]}
- :t2 {:main-opts ["-m" "c199.t2"]}
+ {:t1 {:main-opts ["-m" "c200.t1"]}
+ :t2 {:main-opts ["-m" "c200.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-200/tyler-wardhaugh/clojure/src/c200/t1.clj b/challenge-200/tyler-wardhaugh/clojure/src/c200/t1.clj
new file mode 100644
index 0000000000..7960eecf15
--- /dev/null
+++ b/challenge-200/tyler-wardhaugh/clojure/src/c200/t1.clj
@@ -0,0 +1,29 @@
+(ns c200.t1
+ (:require
+ [clojure.edn :as edn]
+ [clojure.pprint :refer [cl-format]]))
+
+(def DEFAULT-INPUT [[1 2 3 4]])
+
+(defn arithmetic-slices
+ [coll]
+ (let [coll (vec coll)
+ xf (comp (map (fn [[x y]] (- y x)))
+ (map-indexed vector)
+ (partition-by second)
+ (map #(map first %)))
+ runs (->> coll (partition 2 1) (sequence xf))]
+ (for [len (range 2 (count coll))
+ run runs
+ part (partition len 1 run)
+ :let [s (first part)
+ e (-> part last (+ 2))]]
+ (subvec coll s e))))
+
+(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)
+ slices (arithmetic-slices coll)]
+ (cl-format true "~[()~:;~{(~{~a~^,~})~^, ~}~]~%" (count slices) slices)))
diff --git a/challenge-200/tyler-wardhaugh/clojure/src/c200/t2.clj b/challenge-200/tyler-wardhaugh/clojure/src/c200/t2.clj
new file mode 100644
index 0000000000..44e0241c3a
--- /dev/null
+++ b/challenge-200/tyler-wardhaugh/clojure/src/c200/t2.clj
@@ -0,0 +1,10 @@
+(ns c200.t2)
+
+(def DEFAULT-INPUT [200])
+
+(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 parse-long)) DEFAULT-INPUT)]
+ ))
diff --git a/challenge-200/tyler-wardhaugh/clojure/test/c200/t1_test.clj b/challenge-200/tyler-wardhaugh/clojure/test/c200/t1_test.clj
new file mode 100644
index 0000000000..4ec02e6f47
--- /dev/null
+++ b/challenge-200/tyler-wardhaugh/clojure/test/c200/t1_test.clj
@@ -0,0 +1,8 @@
+(ns c200.t1-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [c200.t1 :refer [arithmetic-slices]]))
+
+(deftest task-1
+ (testing "Task 1 produces the correct results from examples in the description"
+ (is (= [[1 2 3] [2 3 4] [1 2 3 4]] (arithmetic-slices [1 2 3 4])))
+ (is (= [] (arithmetic-slices [2])))))
diff --git a/challenge-200/tyler-wardhaugh/clojure/test/c200/t2_test.clj b/challenge-200/tyler-wardhaugh/clojure/test/c200/t2_test.clj
new file mode 100644
index 0000000000..606212ee7a
--- /dev/null
+++ b/challenge-200/tyler-wardhaugh/clojure/test/c200/t2_test.clj
@@ -0,0 +1,7 @@
+(ns c200.t2-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [c200.t2 :refer []]))
+
+(deftest task-2
+ (testing "Task 2 produces the correct results from examples in the description"
+ ))