aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-06-04 22:32:39 +0100
committerGitHub <noreply@github.com>2023-06-04 22:32:39 +0100
commit76db955d662fba216b231f0522edf95d2a38efb9 (patch)
treebedd652cd886f450ca1dbf2d9c5a860672ecdf44
parentcbde6a54a8f112c9d9d0be8d92189f8a3ac2f954 (diff)
parent6c0175228e7cda310caea056001e2eddad380bcf (diff)
downloadperlweeklychallenge-club-76db955d662fba216b231f0522edf95d2a38efb9.tar.gz
perlweeklychallenge-club-76db955d662fba216b231f0522edf95d2a38efb9.tar.bz2
perlweeklychallenge-club-76db955d662fba216b231f0522edf95d2a38efb9.zip
Merge pull request #8181 from tylerw/tw/challenge-219
Ch219: implement Tasks 1 & 2 in Clojure
-rw-r--r--challenge-219/tyler-wardhaugh/clojure/README.md10
-rw-r--r--challenge-219/tyler-wardhaugh/clojure/bb.edn2
-rw-r--r--challenge-219/tyler-wardhaugh/clojure/build.clj19
-rw-r--r--challenge-219/tyler-wardhaugh/clojure/deps.edn8
-rw-r--r--challenge-219/tyler-wardhaugh/clojure/src/c219/t1.clj17
-rw-r--r--challenge-219/tyler-wardhaugh/clojure/src/c219/t2.clj26
-rw-r--r--challenge-219/tyler-wardhaugh/clojure/test/c219/t1_test.clj8
-rw-r--r--challenge-219/tyler-wardhaugh/clojure/test/c219/t2_test.clj8
8 files changed, 89 insertions, 9 deletions
diff --git a/challenge-219/tyler-wardhaugh/clojure/README.md b/challenge-219/tyler-wardhaugh/clojure/README.md
index 130e9d2771..ca9f7b247f 100644
--- a/challenge-219/tyler-wardhaugh/clojure/README.md
+++ b/challenge-219/tyler-wardhaugh/clojure/README.md
@@ -1,6 +1,6 @@
-# c218
+# c219
-The Weekly Challenge — #218 — Tyler Wardhaugh
+The Weekly Challenge — #219 — Tyler Wardhaugh
## Usage
@@ -17,12 +17,12 @@ Run Task #1:
Run Task #2:
- $ clojure -M:t2 COLL
+ $ clojure -M:t2 COSTS DAYS
# ... or ...
- $ bb run task-2 COLL
+ $ bb run task-2 COSTS DAYS
# Alternatively, to run it via Babashka:
- $ bb run task-2-bb COLL
+ $ bb run task-2-bb COSTS DAYS
Run the project's tests (which are samples from the task descriptions):
diff --git a/challenge-219/tyler-wardhaugh/clojure/bb.edn b/challenge-219/tyler-wardhaugh/clojure/bb.edn
index fb73b927b3..f45b64fba7 100644
--- a/challenge-219/tyler-wardhaugh/clojure/bb.edn
+++ b/challenge-219/tyler-wardhaugh/clojure/bb.edn
@@ -1,6 +1,6 @@
{
:paths ["src" "resources"]
- :deps {c218/c218 {:local/root "."}}
+ :deps {c219/c219 {:local/root "."}}
:tasks
{
diff --git a/challenge-219/tyler-wardhaugh/clojure/build.clj b/challenge-219/tyler-wardhaugh/clojure/build.clj
new file mode 100644
index 0000000000..0920bb7051
--- /dev/null
+++ b/challenge-219/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.c219/c219)
+(def version "0.1.0-SNAPSHOT")
+(def main 'c219.c219)
+
+(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-219/tyler-wardhaugh/clojure/deps.edn b/challenge-219/tyler-wardhaugh/clojure/deps.edn
index 3d77f22cca..c8190a62c0 100644
--- a/challenge-219/tyler-wardhaugh/clojure/deps.edn
+++ b/challenge-219/tyler-wardhaugh/clojure/deps.edn
@@ -1,8 +1,10 @@
{:paths ["src" "resources"]
- :deps {org.clojure/clojure {:mvn/version "1.11.1"}}
+ :deps {org.clojure/clojure {:mvn/version "1.11.1"}
+ clojure/math.numeric-tower {:git/url "https://github.com/clojure/math.numeric-tower"
+ :git/sha "3e98b31da229d7d3a533f1cee0c509e9b349aa17"}}
:aliases
- {:t1 {:main-opts ["-m" "c218.t1"]}
- :t2 {:main-opts ["-m" "c218.t2"]}
+ {:t1 {:main-opts ["-m" "c219.t1"]}
+ :t2 {:main-opts ["-m" "c219.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-219/tyler-wardhaugh/clojure/src/c219/t1.clj b/challenge-219/tyler-wardhaugh/clojure/src/c219/t1.clj
new file mode 100644
index 0000000000..43a5fe741b
--- /dev/null
+++ b/challenge-219/tyler-wardhaugh/clojure/src/c219/t1.clj
@@ -0,0 +1,17 @@
+(ns c219.t1
+ (:require
+ [clojure.edn :as edn]
+ [clojure.math.numeric-tower :refer [expt]]))
+
+(def DEFAULT-INPUT [[-2 -1 0 3 4]])
+
+(defn sorted-squares
+ [coll]
+ (->> coll (map #(expt % 2)) sort))
+
+(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 (sorted-squares coll))))
diff --git a/challenge-219/tyler-wardhaugh/clojure/src/c219/t2.clj b/challenge-219/tyler-wardhaugh/clojure/src/c219/t2.clj
new file mode 100644
index 0000000000..b726644926
--- /dev/null
+++ b/challenge-219/tyler-wardhaugh/clojure/src/c219/t2.clj
@@ -0,0 +1,26 @@
+(ns c219.t2
+ (:require
+ [clojure.edn :as edn]))
+
+(def DEFAULT-INPUT [[2 7 25] [1 5 6 7 9 15]])
+
+(defn travel-expenditure'
+ [f costs days]
+ (when (seq days)
+ (let [source (zipmap [1 7 30] costs)
+ xf (map (fn [[period cost]]
+ (let [[d & ds] days
+ candidates (drop-while #(< % (+ d period)) ds)]
+ (+ cost (or (f f costs candidates) 0)))))]
+ (->> source (sequence xf) (apply min)))))
+
+(let [f (memoize travel-expenditure')]
+ (defn travel-expenditure [costs days]
+ (f f costs days)))
+
+(defn -main
+ "Run Task 1 with a given input COSTS and DAYS, defaulting to the first
+ example from the task description."
+ [& args]
+ (let [[costs days] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)]
+ (println (travel-expenditure costs days))))
diff --git a/challenge-219/tyler-wardhaugh/clojure/test/c219/t1_test.clj b/challenge-219/tyler-wardhaugh/clojure/test/c219/t1_test.clj
new file mode 100644
index 0000000000..3c825dd602
--- /dev/null
+++ b/challenge-219/tyler-wardhaugh/clojure/test/c219/t1_test.clj
@@ -0,0 +1,8 @@
+(ns c219.t1-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [c219.t1 :refer [sorted-squares]]))
+
+(deftest task-1
+ (testing "Task 1 produces the correct results from examples in the description"
+ (is (= [0 1 4 9 16] (sorted-squares [-2 -1 0 3 4])))
+ (is (= [1 9 16 25 36] (sorted-squares [5 -4 -1 3 6])))))
diff --git a/challenge-219/tyler-wardhaugh/clojure/test/c219/t2_test.clj b/challenge-219/tyler-wardhaugh/clojure/test/c219/t2_test.clj
new file mode 100644
index 0000000000..1f1a24b3cf
--- /dev/null
+++ b/challenge-219/tyler-wardhaugh/clojure/test/c219/t2_test.clj
@@ -0,0 +1,8 @@
+(ns c219.t2-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [c219.t2 :refer [travel-expenditure]]))
+
+(deftest task-1
+ (testing "Task 2 produces the correct results from examples in the description"
+ (is (= 11 (travel-expenditure [2 7 25] [1 5 6 7 9 15])))
+ (is (= 20 (travel-expenditure [2 7 25] [1 2 3 5 7 10 11 12 14 20 30 31])))))