diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-12-27 08:47:29 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-27 08:47:29 +0000 |
| commit | ca6c62792484ec1fa6ad270e670dfcbd4f95cb19 (patch) | |
| tree | 725cf51c67c65d3c1e7afdd31990a709411bb2d2 | |
| parent | d56ae39c4b472d2f22bfe7cab5d68692235d505f (diff) | |
| parent | 637b009a23a02c2475fd8098d57c1f920352a585 (diff) | |
| download | perlweeklychallenge-club-ca6c62792484ec1fa6ad270e670dfcbd4f95cb19.tar.gz perlweeklychallenge-club-ca6c62792484ec1fa6ad270e670dfcbd4f95cb19.tar.bz2 perlweeklychallenge-club-ca6c62792484ec1fa6ad270e670dfcbd4f95cb19.zip | |
Merge pull request #3071 from tylerw/tw/challenge-092
Challenge 092
5 files changed, 74 insertions, 14 deletions
diff --git a/challenge-092/tyler-wardhaugh/clojure/README.md b/challenge-092/tyler-wardhaugh/clojure/README.md index 32467a13d1..c83f0cfefa 100644 --- a/challenge-092/tyler-wardhaugh/clojure/README.md +++ b/challenge-092/tyler-wardhaugh/clojure/README.md @@ -1,13 +1,13 @@ -# tw.weekly.c91 +# tw.weekly.c92 -The Weekly Challenge - #091 - Tyler Wardhaugh +The Weekly Challenge - #092 - Tyler Wardhaugh ## Usage Run the project directly (shows default output from both tasks): - $ clojure -M -m tw.weekly.c91.core + $ clojure -M -m tw.weekly.c92.core Run the project's tests (which are samples from the task descriptions): @@ -15,11 +15,11 @@ Run the project's tests (which are samples from the task descriptions): Run Task #1 with input - $ clojure -M -m tw.weekly.c91.t1 N + $ clojure -M -m tw.weekly.c92.t1 A B Run Task #2: - $ clojure -M -m tw.weekly.c91.t2 N1 N2 N3... + $ clojure -M -m tw.weekly.c92.t2 N S ## Project Template diff --git a/challenge-092/tyler-wardhaugh/clojure/deps.edn b/challenge-092/tyler-wardhaugh/clojure/deps.edn index e89f0d9fff..5a4409846b 100644 --- a/challenge-092/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-092/tyler-wardhaugh/clojure/deps.edn @@ -10,5 +10,5 @@ :main-opts ["-m" "cognitect.test-runner" "-d" "test"]} :uberjar {:extra-deps {seancorfield/depstar {:mvn/version "1.0.94"}} - :main-opts ["-m" "hf.depstar.uberjar" "tw.weekly.c91.jar" - "-C" "-m" "tw.weekly.c91"]}}} + :main-opts ["-m" "hf.depstar.uberjar" "tw.weekly.c92.jar" + "-C" "-m" "tw.weekly.c92"]}}} diff --git a/challenge-092/tyler-wardhaugh/clojure/pom.xml b/challenge-092/tyler-wardhaugh/clojure/pom.xml index 88c2883d84..5dc7120821 100644 --- a/challenge-092/tyler-wardhaugh/clojure/pom.xml +++ b/challenge-092/tyler-wardhaugh/clojure/pom.xml @@ -2,11 +2,11 @@ <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>tw.weekly</groupId> - <artifactId>tw.weekly.c91</artifactId> + <artifactId>tw.weekly.c92</artifactId> <version>0.1.0-SNAPSHOT</version> - <name>tw.weekly.c91</name> - <description>Challenge #091</description> - <url>https://github.com/tw.weekly/tw.weekly.c91</url> + <name>tw.weekly.c92</name> + <description>Challenge #092</description> + <url>https://github.com/tw.weekly/tw.weekly.c92</url> <licenses> <license> <name>Eclipse Public License</name> @@ -19,9 +19,9 @@ </developer> </developers> <scm> - <url>https://github.com/tw.weekly/tw.weekly.c91</url> - <connection>scm:git:git://github.com/tw.weekly/tw.weekly.c91.git</connection> - <developerConnection>scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c91.git</developerConnection> + <url>https://github.com/tw.weekly/tw.weekly.c92</url> + <connection>scm:git:git://github.com/tw.weekly/tw.weekly.c92.git</connection> + <developerConnection>scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c92.git</developerConnection> <tag>HEAD</tag> </scm> <dependencies> diff --git a/challenge-092/tyler-wardhaugh/clojure/src/tw/weekly/c92/t1.clj b/challenge-092/tyler-wardhaugh/clojure/src/tw/weekly/c92/t1.clj new file mode 100644 index 0000000000..bd8332ce54 --- /dev/null +++ b/challenge-092/tyler-wardhaugh/clojure/src/tw/weekly/c92/t1.clj @@ -0,0 +1,22 @@ +(ns tw.weekly.c92.t1 + (:require [clojure.edn :as edn])) + +;;; +; Task description for TASK #1 › Isomorphic Strings +;;; + +(def DEFAULT-INPUT ["abc" "xyz"]) + +(defn isomorphic? + "Determine if two strings are isomorphic." + [a b] + (let [avals (-> a frequencies vals sort) + bvals (-> b frequencies vals sort)] + (= avals bvals))) + +(defn -main + "Run Task 1 using two given strings, defaulting to the example given in + the task description." + [& args] + (let [[A B] (or (some->> args (take 2) edn/read-string) DEFAULT-INPUT)] + (println (if (isomorphic? A B) 1 0)))) diff --git a/challenge-092/tyler-wardhaugh/clojure/src/tw/weekly/c92/t2.clj b/challenge-092/tyler-wardhaugh/clojure/src/tw/weekly/c92/t2.clj new file mode 100644 index 0000000000..364130edcc --- /dev/null +++ b/challenge-092/tyler-wardhaugh/clojure/src/tw/weekly/c92/t2.clj @@ -0,0 +1,38 @@ +(ns tw.weekly.c92.t2 + (:require [clojure.edn :as edn] + [clojure.pprint :refer [cl-format]])) + +;;; +; Task description for TASK #2 › Insert Interval +;;; + +(def DEFAULT-INPUT {:N [2 6] :S [[1 4], [8 10]]}) + +(defn insert-interval + "Insert an interval `n` into a sorted sequence of non-overlapping intervals `s`." + [n s] + (let [[n1 n2] n + get-head (fn [coll] (ffirst coll)) + get-tail (fn [coll] (-> coll last second))] + (cond (< n2 (get-head s)) (concat [n] s) + (> n1 (get-tail s)) (concat s [n]) + :else + (let [xf (map (fn [[c1 c2 :as c]] [c (<= n1 c2) (>= n2 c1)])) + step1 (eduction xf s) + just-h (keep (fn [[c x _]] (when x c))) + just-t (keep (fn [[c _ y]] (when y c))) + leading (keep (fn [[c x y]] (when (and (not x) y) c))) + lagging (keep (fn [[c x y]] (when (and x (not y)) c))) + head (min n1 (get-head (sequence just-h step1))) + tail (max n2 (get-tail (sequence just-t step1)))] + (concat (sequence leading step1) + [[head tail]] + (sequence lagging step1)))))) + +(defn -main + "Run Task 2 with the given positive integers, defaulting to the example + given in the explanation page linked from the task description." + [& args] + (let [N (or (some-> args first edn/read-string vec) (:N DEFAULT-INPUT)) + S (or (some->> args second edn/read-string) (:S DEFAULT-INPUT))] + (cl-format true "~{(~{~a~^,~})~^, ~}~%" (insert-interval N S)))) |
