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
commit01d318f579ad3fdae1b796d57e83daccbe2c4198 (patch)
treed95687be19005ec22a50150bef0dae08e32d0f4c
parent952dfbc2740e2cd1d13dd22c3367c4f9bb11acfc (diff)
downloadperlweeklychallenge-club-01d318f579ad3fdae1b796d57e83daccbe2c4198.tar.gz
perlweeklychallenge-club-01d318f579ad3fdae1b796d57e83daccbe2c4198.tar.bz2
perlweeklychallenge-club-01d318f579ad3fdae1b796d57e83daccbe2c4198.zip
Ch81: Task 2
-rw-r--r--challenge-081/tyler-wardhaugh/clojure/deps.edn3
-rw-r--r--challenge-081/tyler-wardhaugh/clojure/pom.xml5
-rw-r--r--challenge-081/tyler-wardhaugh/clojure/resources/input3
-rw-r--r--challenge-081/tyler-wardhaugh/clojure/src/tw/weekly/c81/core.clj5
-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.clj15
6 files changed, 60 insertions, 3 deletions
diff --git a/challenge-081/tyler-wardhaugh/clojure/deps.edn b/challenge-081/tyler-wardhaugh/clojure/deps.edn
index d74986e08e..8bbe260e55 100644
--- a/challenge-081/tyler-wardhaugh/clojure/deps.edn
+++ b/challenge-081/tyler-wardhaugh/clojure/deps.edn
@@ -1,5 +1,6 @@
{:paths ["src" "resources"]
- :deps {org.clojure/clojure {:mvn/version "1.10.1"}}
+ :deps {org.clojure/clojure {:mvn/version "1.10.1"}
+ net.cgrand/xforms {:mvn/version "0.19.2"}}
:aliases
{:test {:extra-paths ["test"]
:extra-deps {org.clojure/test.check {:mvn/version "1.0.0"}}}
diff --git a/challenge-081/tyler-wardhaugh/clojure/pom.xml b/challenge-081/tyler-wardhaugh/clojure/pom.xml
index 20a672bad9..a997eb3a20 100644
--- a/challenge-081/tyler-wardhaugh/clojure/pom.xml
+++ b/challenge-081/tyler-wardhaugh/clojure/pom.xml
@@ -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
index a0973e56a1..146c6bd5fd 100644
--- a/challenge-081/tyler-wardhaugh/clojure/src/tw/weekly/c81/core.clj
+++ b/challenge-081/tyler-wardhaugh/clojure/src/tw/weekly/c81/core.clj
@@ -1,9 +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))
+ (t1/-main)
+ (println "Task #2")
+ (t2/-main))
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
index a9f3889aa1..4126410f68 100644
--- a/challenge-081/tyler-wardhaugh/clojure/test/tw/weekly/c81_test.clj
+++ b/challenge-081/tyler-wardhaugh/clojure/test/tw/weekly/c81_test.clj
@@ -1,8 +1,21 @@
(ns tw.weekly.c81-test
(:require [clojure.test :refer [deftest is testing]]
- [tw.weekly.c81.t1 :refer [common-base-string]]))
+ [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"})})))))