From 952dfbc2740e2cd1d13dd22c3367c4f9bb11acfc Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Tue, 6 Oct 2020 08:56:51 -0700 Subject: Ch81: Task 1 --- .../clojure/src/tw/weekly/c81/core.clj | 9 ++++++++ .../clojure/src/tw/weekly/c81/t1.clj | 25 ++++++++++++++++++++++ .../clojure/test/tw/weekly/c81_test.clj | 8 +++++++ 3 files changed, 42 insertions(+) create mode 100644 challenge-081/tyler-wardhaugh/clojure/src/tw/weekly/c81/core.clj create mode 100644 challenge-081/tyler-wardhaugh/clojure/src/tw/weekly/c81/t1.clj create mode 100644 challenge-081/tyler-wardhaugh/clojure/test/tw/weekly/c81_test.clj 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"])))) -- cgit