aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTyler Wardhaugh <tyler.wardhaugh@gmail.com>2020-10-06 08:56:51 -0700
committerTyler Wardhaugh <tyler.wardhaugh@gmail.com>2020-10-06 10:07:07 -0700
commit952dfbc2740e2cd1d13dd22c3367c4f9bb11acfc (patch)
treeff8e098b9454e5aac7600bdae2c5c0da8b628f09
parentbafce2336b7462f722bdbe728674273417e30fff (diff)
downloadperlweeklychallenge-club-952dfbc2740e2cd1d13dd22c3367c4f9bb11acfc.tar.gz
perlweeklychallenge-club-952dfbc2740e2cd1d13dd22c3367c4f9bb11acfc.tar.bz2
perlweeklychallenge-club-952dfbc2740e2cd1d13dd22c3367c4f9bb11acfc.zip
Ch81: Task 1
-rw-r--r--challenge-081/tyler-wardhaugh/clojure/src/tw/weekly/c81/core.clj9
-rw-r--r--challenge-081/tyler-wardhaugh/clojure/src/tw/weekly/c81/t1.clj25
-rw-r--r--challenge-081/tyler-wardhaugh/clojure/test/tw/weekly/c81_test.clj8
3 files changed, 42 insertions, 0 deletions
diff --git a/challenge-081/tyler-wardhaugh/clojure/src/tw/weekly/c81/core.clj b/challenge-081/tyler-wardhaugh/clojure/src/tw/weekly/c81/core.clj
new file mode 100644
index 0000000000..a0973e56a1
--- /dev/null
+++ b/challenge-081/tyler-wardhaugh/clojure/src/tw/weekly/c81/core.clj
@@ -0,0 +1,9 @@
+(ns tw.weekly.c81.core
+ (:require [tw.weekly.c81.t1 :as t1])
+ (:gen-class))
+
+(defn -main
+ "Run all tasks"
+ [& _]
+ (println "Task #1")
+ (t1/-main))
diff --git a/challenge-081/tyler-wardhaugh/clojure/src/tw/weekly/c81/t1.clj b/challenge-081/tyler-wardhaugh/clojure/src/tw/weekly/c81/t1.clj
new file mode 100644
index 0000000000..13a382f7c6
--- /dev/null
+++ b/challenge-081/tyler-wardhaugh/clojure/src/tw/weekly/c81/t1.clj
@@ -0,0 +1,25 @@
+(ns tw.weekly.c81.t1)
+
+;;; Task description for TASK #1 › Common Base String
+; You are given 2 strings, $A and $B.
+;
+; Write a script to find out common base strings in $A and $B.
+;
+; >>> A substring of a string $S is called base string if repeated concatenation of the substring results in the string.;
+;;;
+
+(defn common-base-string
+ "Find the common base string of two strings."
+ [s1 s2]
+ (let [[[_ small] [large-len large]] (->> [s1 s2] (map (juxt count identity)) sort)
+ is-substring (fn [s] (= large (reduce str (-> (quot large-len (count s)) (repeat s)))))]
+ (->> (reductions str "" small)
+ (drop 1)
+ (filter is-substring))))
+
+(defn -main
+ "Run Task 1 with two strings A and B, defaulting to the first example given in the task description."
+ [& args]
+ (let [[A B] (or (some->> args (take 2)) ["abcdabcd" "abcdabcdabcdabcd"])
+ base (common-base-string A B)]
+ (println base)))
diff --git a/challenge-081/tyler-wardhaugh/clojure/test/tw/weekly/c81_test.clj b/challenge-081/tyler-wardhaugh/clojure/test/tw/weekly/c81_test.clj
new file mode 100644
index 0000000000..a9f3889aa1
--- /dev/null
+++ b/challenge-081/tyler-wardhaugh/clojure/test/tw/weekly/c81_test.clj
@@ -0,0 +1,8 @@
+(ns tw.weekly.c81-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [tw.weekly.c81.t1 :refer [common-base-string]]))
+
+(deftest task-1
+ (testing "Task 1, Common Base String"
+ (is (= (common-base-string "abcdabcd" "abcdabcdabcdabcd") ["abcd" "abcdabcd"]))
+ (is (= (common-base-string "aaa" "aa") ["a"]))))