aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-10-07 09:29:55 +0100
committerGitHub <noreply@github.com>2020-10-07 09:29:55 +0100
commit904f1b93bd123ca444804fd8b22d6d7357de7238 (patch)
tree4a1e2de2d80568d7401a1e22b54f30ee3f3b396a
parentc8fbab0f69aed77a65cfb4aa784703acb2075ffe (diff)
parent01d318f579ad3fdae1b796d57e83daccbe2c4198 (diff)
downloadperlweeklychallenge-club-904f1b93bd123ca444804fd8b22d6d7357de7238.tar.gz
perlweeklychallenge-club-904f1b93bd123ca444804fd8b22d6d7357de7238.tar.bz2
perlweeklychallenge-club-904f1b93bd123ca444804fd8b22d6d7357de7238.zip
Merge pull request #2468 from tylerw/tw/challenge-081
Challenge 081
-rw-r--r--challenge-081/tyler-wardhaugh/clojure/README.md16
-rw-r--r--challenge-081/tyler-wardhaugh/clojure/deps.edn6
-rw-r--r--challenge-081/tyler-wardhaugh/clojure/pom.xml19
-rw-r--r--challenge-081/tyler-wardhaugh/clojure/resources/input3
-rw-r--r--challenge-081/tyler-wardhaugh/clojure/src/tw/weekly/c81/core.clj12
-rw-r--r--challenge-081/tyler-wardhaugh/clojure/src/tw/weekly/c81/t1.clj25
-rw-r--r--challenge-081/tyler-wardhaugh/clojure/src/tw/weekly/c81/t2.clj32
-rw-r--r--challenge-081/tyler-wardhaugh/clojure/test/tw/weekly/c81_test.clj21
8 files changed, 114 insertions, 20 deletions
diff --git a/challenge-081/tyler-wardhaugh/clojure/README.md b/challenge-081/tyler-wardhaugh/clojure/README.md
index 27344ef9cb..3a0c44fe10 100644
--- a/challenge-081/tyler-wardhaugh/clojure/README.md
+++ b/challenge-081/tyler-wardhaugh/clojure/README.md
@@ -1,29 +1,25 @@
-# tw.weekly.c80
+# tw.weekly.c81
-The Weekly Challenge - #080 - Tyler Wardhaugh
+The Weekly Challenge - #081 - Tyler Wardhaugh
## Usage
Run the project directly (shows default output from both tasks):
- $ clojure -m tw.weekly.c80.core
+ $ clojure -M -m tw.weekly.c81.core
Run the project's tests (which are samples from the task descriptions):
- $ clojure -A:test:runner
+ $ clojure -M:test:runner
Run Task #1 with input
- $ clojure -m tw.weekly.c80.t1 A1 A2 A3 [...]
-
-Benchmark both methods defined in Task #1:
-
- $ clojure -m tw.weekly.c80.t1-bench
+ $ clojure -M -m tw.weekly.c81.t1 A B
Run Task #2 with input:
- $ clojure -m tw.weekly.c80.t2 A1 A2 A3 [...]
+ $ clojure -M -m tw.weekly.c81.t2 INPUT-FILE
## Project Template
diff --git a/challenge-081/tyler-wardhaugh/clojure/deps.edn b/challenge-081/tyler-wardhaugh/clojure/deps.edn
index c572dcdfc7..8bbe260e55 100644
--- a/challenge-081/tyler-wardhaugh/clojure/deps.edn
+++ b/challenge-081/tyler-wardhaugh/clojure/deps.edn
@@ -1,6 +1,6 @@
{:paths ["src" "resources"]
:deps {org.clojure/clojure {:mvn/version "1.10.1"}
- criterium/criterium {:mvn/version "0.4.6"}}
+ net.cgrand/xforms {:mvn/version "0.19.2"}}
:aliases
{:test {:extra-paths ["test"]
:extra-deps {org.clojure/test.check {:mvn/version "1.0.0"}}}
@@ -11,5 +11,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.c80.jar"
- "-C" "-m" "tw.weekly.c80"]}}}
+ :main-opts ["-m" "hf.depstar.uberjar" "tw.weekly.c81.jar"
+ "-C" "-m" "tw.weekly.c81"]}}}
diff --git a/challenge-081/tyler-wardhaugh/clojure/pom.xml b/challenge-081/tyler-wardhaugh/clojure/pom.xml
index 525b004ca8..a997eb3a20 100644
--- a/challenge-081/tyler-wardhaugh/clojure/pom.xml
+++ b/challenge-081/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.c80</artifactId>
+ <artifactId>tw.weekly.c81</artifactId>
<version>0.1.0-SNAPSHOT</version>
- <name>tw.weekly.c80</name>
- <description>Challenge #080</description>
- <url>https://github.com/tw.weekly/tw.weekly.c80</url>
+ <name>tw.weekly.c81</name>
+ <description>Challenge #081</description>
+ <url>https://github.com/tw.weekly/tw.weekly.c81</url>
<licenses>
<license>
<name>Eclipse Public License</name>
@@ -19,9 +19,9 @@
</developer>
</developers>
<scm>
- <url>https://github.com/tw.weekly/tw.weekly.c80</url>
- <connection>scm:git:git://github.com/tw.weekly/tw.weekly.c80.git</connection>
- <developerConnection>scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c80.git</developerConnection>
+ <url>https://github.com/tw.weekly/tw.weekly.c81</url>
+ <connection>scm:git:git://github.com/tw.weekly/tw.weekly.c81.git</connection>
+ <developerConnection>scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c81.git</developerConnection>
<tag>HEAD</tag>
</scm>
<dependencies>
@@ -30,6 +30,11 @@
<artifactId>clojure</artifactId>
<version>1.10.1</version>
</dependency>
+ <dependency>
+ <groupId>net.cgrand</groupId>
+ <artifactId>xforms</artifactId>
+ <version>0.19.2</version>
+ </dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
diff --git a/challenge-081/tyler-wardhaugh/clojure/resources/input b/challenge-081/tyler-wardhaugh/clojure/resources/input
new file mode 100644
index 0000000000..37001629ad
--- /dev/null
+++ b/challenge-081/tyler-wardhaugh/clojure/resources/input
@@ -0,0 +1,3 @@
+West Side Story
+
+The award-winning adaptation of the classic romantic tragedy "Romeo and Juliet". The feuding families become two warring New York City gangs, the white Jets led by Riff and the Latino Sharks, led by Bernardo. Their hatred escalates to a point where neither can coexist with any form of understanding. But when Riff's best friend (and former Jet) Tony and Bernardo's younger sister Maria meet at a dance, no one can do anything to stop their love. Maria and Tony begin meeting in secret, planning to run away. Then the Sharks and Jets plan a rumble under the highway--whoever wins gains control of the streets. Maria sends Tony to stop it, hoping it can end the violence. It goes terribly wrong, and before the lovers know what's happened, tragedy strikes and doesn't stop until the climactic and heartbreaking ending.
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..146c6bd5fd
--- /dev/null
+++ b/challenge-081/tyler-wardhaugh/clojure/src/tw/weekly/c81/core.clj
@@ -0,0 +1,12 @@
+(ns tw.weekly.c81.core
+ (:require [tw.weekly.c81.t1 :as t1])
+ (:require [tw.weekly.c81.t2 :as t2])
+ (:gen-class))
+
+(defn -main
+ "Run all tasks"
+ [& _]
+ (println "Task #1")
+ (t1/-main)
+ (println "Task #2")
+ (t2/-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/src/tw/weekly/c81/t2.clj b/challenge-081/tyler-wardhaugh/clojure/src/tw/weekly/c81/t2.clj
new file mode 100644
index 0000000000..543a1f54b6
--- /dev/null
+++ b/challenge-081/tyler-wardhaugh/clojure/src/tw/weekly/c81/t2.clj
@@ -0,0 +1,32 @@
+(ns tw.weekly.c81.t2
+ (:require [clojure.java.io :as io])
+ (:require [clojure.pprint :refer [cl-format]])
+ (:require [clojure.string :as str])
+ (:require [net.cgrand.xforms :as x])
+ (:require [net.cgrand.xforms.io :as xio]))
+
+;;; Task description for TASK #2 › Frequency Sort
+; You are given file named input.
+;
+; Write a script to find the frequency of all the words.
+;
+; It should print the result as first column of each line should be the frequency of the the word followed by all the words of that frequency arranged in lexicographical order. Also sort the words in the ascending order of frequency.
+;;;
+
+(defn word-frequency-sort
+ "Return a sorted word frequency map for a given text."
+ [source]
+ (let [cleaner (fn [s] (str/replace s #"(?:[.\"\(\),]|'s|--|\n)" " "))
+ splitter (fn [s] (str/split s #" "))
+ xf (comp (mapcat (comp splitter cleaner))
+ (remove #{""})
+ (x/by-key identity (x/into []))
+ (x/by-key (comp count second) first (x/into (sorted-set))))]
+ (into (sorted-map) xf source)))
+
+(defn -main
+ "Run Task 2 with an input, defaulting to the example given in the task description."
+ [& args]
+ (let [input (or (some->> args first io/file) (io/resource "input"))
+ freqs (word-frequency-sort (xio/lines-in input))]
+ (cl-format true "~:{~a ~{~a~^ ~}~%~^~%~}" freqs)))
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..4126410f68
--- /dev/null
+++ b/challenge-081/tyler-wardhaugh/clojure/test/tw/weekly/c81_test.clj
@@ -0,0 +1,21 @@
+(ns tw.weekly.c81-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [clojure.java.io :as io]
+ [net.cgrand.xforms.io :as xio]
+ [tw.weekly.c81.t1 :refer [common-base-string]]
+ [tw.weekly.c81.t2 :refer [word-frequency-sort]]))
+
+(deftest task-1
+ (testing "Task 1, Common Base String"
+ (is (= (common-base-string "abcdabcd" "abcdabcdabcdabcd") ["abcd" "abcdabcd"]))
+ (is (= (common-base-string "aaa" "aa") ["a"]))))
+
+(deftest task-2
+ (testing "Task 2, Frequency Sort"
+ (is (= (word-frequency-sort (xio/lines-in (io/resource "input")))
+ (into (sorted-map)
+ {1 (into (sorted-set) #{"But" "City" "It" "Jet" "Juliet" "Latino" "New" "Romeo" "Side" "Story" "Their" "Then" "West" "York" "adaptation" "any" "anything" "at" "award-winning" "away" "become" "before" "begin" "best" "classic" "climactic" "coexist" "control" "dance" "do" "doesn't" "end" "ending" "escalates" "families" "feuding" "form" "former" "friend" "gains" "gangs" "goes" "happened" "hatred" "heartbreaking" "highway" "hoping" "in" "know" "love" "lovers" "meet" "meeting" "neither" "no" "one" "plan" "planning" "point" "romantic" "rumble" "run" "secret" "sends" "sister" "streets" "strikes" "terribly" "their" "two" "under" "understanding" "until" "violence" "warring" "what" "when" "where" "white" "whoever" "wins" "with" "wrong" "younger"})
+ 2 (into (sorted-set) #{"Bernardo" "Jets" "Riff" "Sharks" "The" "by" "it" "led" "tragedy"})
+ 3 (into (sorted-set) #{"Maria" "Tony" "a" "can" "of" "stop"})
+ 4 #{"to"}
+ 9 (into (sorted-set) #{"and" "the"})})))))