aboutsummaryrefslogtreecommitdiff
path: root/challenge-134
diff options
context:
space:
mode:
authorTyler Wardhaugh <twardhaugh@cap-rx.com>2021-10-15 12:39:43 -0700
committerTyler Wardhaugh <twardhaugh@cap-rx.com>2021-10-16 12:28:04 -0700
commit7689e9c56fc326d5ede2f1077cd521e915446232 (patch)
tree3890d0005bca6452adda77f2241dcf09313eabae /challenge-134
parentd64fa3b6f38828d06312399c58a857a4e2190bec (diff)
downloadperlweeklychallenge-club-7689e9c56fc326d5ede2f1077cd521e915446232.tar.gz
perlweeklychallenge-club-7689e9c56fc326d5ede2f1077cd521e915446232.tar.bz2
perlweeklychallenge-club-7689e9c56fc326d5ede2f1077cd521e915446232.zip
Ch134 (Clojure): Task 2
Diffstat (limited to 'challenge-134')
-rw-r--r--challenge-134/tyler-wardhaugh/clojure/README.md4
-rw-r--r--challenge-134/tyler-wardhaugh/clojure/src/tw/weekly/c134/t2.clj52
-rw-r--r--challenge-134/tyler-wardhaugh/clojure/test/tw/weekly/c134/t2_test.clj11
3 files changed, 57 insertions, 10 deletions
diff --git a/challenge-134/tyler-wardhaugh/clojure/README.md b/challenge-134/tyler-wardhaugh/clojure/README.md
index e63c5ea04a..1293119417 100644
--- a/challenge-134/tyler-wardhaugh/clojure/README.md
+++ b/challenge-134/tyler-wardhaugh/clojure/README.md
@@ -25,9 +25,9 @@ Run Task #1 with input
Run Task #2 with input:
- $ clojure -M -m tw.weekly.c134.t2 N
+ $ clojure -M -m tw.weekly.c134.t2 M N
# ... or ...
- $ bb run task-2 N
+ $ bb run task-2 M N
View available tasks Babashka can run:
diff --git a/challenge-134/tyler-wardhaugh/clojure/src/tw/weekly/c134/t2.clj b/challenge-134/tyler-wardhaugh/clojure/src/tw/weekly/c134/t2.clj
index 72aeb85a7f..8cd893303d 100644
--- a/challenge-134/tyler-wardhaugh/clojure/src/tw/weekly/c134/t2.clj
+++ b/challenge-134/tyler-wardhaugh/clojure/src/tw/weekly/c134/t2.clj
@@ -1,14 +1,52 @@
(ns tw.weekly.c134.t2
- (:require [clojure.edn :as edn]))
+ (:require [clojure.edn :as edn]
+ [clojure.string :as str]
+ [clojure.pprint :refer [cl-format print-table]]))
;;;
-; Task description for TASK #2 ›
+; Task description for TASK #2 › Distinct Terms Count
;;;
-(def DEFAULT-INPUT [])
+(def DEFAULT-INPUT [3 3])
+
+;;;
+; Clojure provides a built in pretty printer for tables, which is similar to
+; the output requested in the challenge, so let's use that and modify it as
+; necessary.
+;
+; First we transform the input into what the pretty printer expects by
+; converting a table (list-of-lists) to a list of sorted maps where the key is
+; the index and value is unchanged, after prepending each with the inner list
+; count (starting from 1). After feeding that into the printer, we capture it
+; and modify it to our needs.
+;;;
+(defn table-str
+ "Return a pretty-printable representation of table."
+ [table]
+ (let [xf (map-indexed
+ (fn [i v] (into (sorted-map)
+ (map-indexed vector)
+ (conj (seq v) (inc i)))))]
+ (-> (with-out-str (print-table (sequence xf table)))
+ (str/replace-first \0 \x)
+ (str/replace #"(?m)(^\||\|$)" " ")
+ (str/replace #"(?<=\+.*)\+" "-")
+ (str/replace #"(?<=\|.*)\|" " "))))
+
+(defn gen-mult-table
+ "Return a vector of [table distinct-terms], where table is a list-of-lists
+ representing the multiplication table of MxN and distinct-terms is a set of
+ all the terms in said table."
+ [M N]
+ (let [base (for [m (range M), n (range N)] (* (inc m) (inc n)))]
+ (vector (partition N base) (set base))))
(defn -main
- "Run Task 1 with a given input N, defaulting to the first example from the
- task description."
+ "Run Task 1 with a given input M and N, defaulting to the first example from
+ the task description."
[& args]
- (let [[N] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)]
- ))
+ (let [[M N] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)
+ [table distinct-terms] (gen-mult-table M N)]
+ (cl-format true "~a~%Distinct Terms: ~{~a~^, ~}~%Count: ~a~%"
+ (table-str table)
+ (sort distinct-terms)
+ (count distinct-terms))))
diff --git a/challenge-134/tyler-wardhaugh/clojure/test/tw/weekly/c134/t2_test.clj b/challenge-134/tyler-wardhaugh/clojure/test/tw/weekly/c134/t2_test.clj
index 4ba20fcb26..befa10895e 100644
--- a/challenge-134/tyler-wardhaugh/clojure/test/tw/weekly/c134/t2_test.clj
+++ b/challenge-134/tyler-wardhaugh/clojure/test/tw/weekly/c134/t2_test.clj
@@ -1,3 +1,12 @@
(ns tw.weekly.c134.t2-test
(:require [clojure.test :refer [deftest is testing]]
- [tw.weekly.c134.t2 :refer []]))
+ [tw.weekly.c134.t2 :refer [gen-mult-table]]))
+
+(deftest examples
+ (testing "Examples from description"
+ (is (= ['((1 2 3) (2 4 6) (3 6 9))
+ #{1 4 6 3 2 9}]
+ (gen-mult-table 3 3)))
+ (is (= ['((1 2 3 4 5) (2 4 6 8 10) (3 6 9 12 15))
+ #{1 4 15 6 3 12 2 9 5 10 8}]
+ (gen-mult-table 3 5)))))