diff --git a/pom.xml b/pom.xml index e66b725..efb68c4 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,18 @@ io.zipcoder collections 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + diff --git a/src/main/java/io/zipcoder/ParenChecker.java b/src/main/java/io/zipcoder/ParenChecker.java index caee675..171b2d7 100644 --- a/src/main/java/io/zipcoder/ParenChecker.java +++ b/src/main/java/io/zipcoder/ParenChecker.java @@ -1,4 +1,61 @@ package io.zipcoder; +import java.util.Stack; + public class ParenChecker { + + public Stack stack = new Stack(); + + public ParenChecker() { + } + + public ParenChecker(Stack stack) { + this.stack = stack; + } + + public boolean checkParen(String str) + { + Stack stack = new Stack(); + for (int i = 0; i < str.length(); i++) + { + char current = str.charAt(i); + if (current == '{' || current == '(' || current == '[' || current == '<' || current == '"' || current == '\'') + { + stack.push(current); + } + + if (current == '}' || current == ')' || current == ']' || current == '>' || current == '"' || current == '\'') + { + if (stack.isEmpty()) + return false; + + char last = stack.peek(); + if (current == '}' && last == '{' || current == ')' && last == '(' || current == ']' && last == '[' || current == '>' && last == '<' || current == '"' && last == '"' || current == '\'' && last == '\'') + stack.pop(); + else + return false; + } + } + return stack.isEmpty(); + } + } +// public boolean checkParen(String str) { +// int opening = 0; +// int closing = 0; +// int total; +// for (int i = 0; i < str.length(); i++) { +// char current = str.charAt(i); +// if (current == '(') { +// opening++; +// } +// if (current == ')') { +// closing++; +// } +// total = opening - closing; +// if (total == 0) { +// return true; +// } +// } +// return false; +// } \ No newline at end of file diff --git a/src/main/java/io/zipcoder/WC.java b/src/main/java/io/zipcoder/WC.java index babb68c..bacad29 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -2,11 +2,15 @@ import java.io.FileNotFoundException; import java.io.FileReader; -import java.util.Iterator; -import java.util.Scanner; +import java.util.*; + +import static java.util.stream.Collectors.toMap; + public class WC { private Iterator si; + public Map wordMap = new LinkedHashMap(); + public WC(String fileName) { try { @@ -20,4 +24,33 @@ public WC(String fileName) { public WC(Iterator si) { this.si = si; } + + public Map wordCountStorage() { + + while (si.hasNext()) { + String current = si.next().toLowerCase().replaceAll("[^a-z0-9]", ""); + if (!wordMap.containsKey(current)) { + wordMap.put(current, 1); + } else { + wordMap.put(current, wordMap.get(current) + 1); + } + } + return wordMap; + } + + public Map sortInDescOrderByValue() { + Map sortByDesc = wordCountStorage().entrySet() + .stream() + .sorted(Map.Entry.comparingByValue().reversed()) + .collect(toMap(Map.Entry::getKey, + Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new)); + + return sortByDesc; + } } + + + + + +//TreeMap uniqueWordCount = new TreeMap(); diff --git a/src/main/resources/robertFrost.txt b/src/main/resources/robertFrost.txt new file mode 100644 index 0000000..50132fe --- /dev/null +++ b/src/main/resources/robertFrost.txt @@ -0,0 +1,23 @@ +Two roads diverged in a yellow wood, +And sorry I could not travel both +And be one traveler, long I stood +And looked down one as far as I could +To where it bent in the undergrowth; + +Then took the other, as just as fair, +And having perhaps the better claim, +Because it was grassy and wanted wear; +Though as for that the passing there +Had worn them really about the same, + +And both that morning equally lay +In leaves no step had trodden black. +Oh, I kept the first for another day! +Yet knowing how way leads on to way, +I doubted if I should ever come back. + +I shall be telling this with a sigh +Somewhere ages and ages hence: +Two roads diverged in a wood, and I-- +I took the one less traveled by, +And that has made all the difference. \ No newline at end of file diff --git a/src/main/resources/someTextFile.txt b/src/main/resources/someTextFile.txt index e69de29..4e95490 100644 --- a/src/main/resources/someTextFile.txt +++ b/src/main/resources/someTextFile.txt @@ -0,0 +1 @@ +Hello my name is Keith. I am a person. I am 26. \ No newline at end of file diff --git a/src/test/java/io/zipcoder/ParenCheckerTest.java b/src/test/java/io/zipcoder/ParenCheckerTest.java index 76aa3b6..3228269 100644 --- a/src/test/java/io/zipcoder/ParenCheckerTest.java +++ b/src/test/java/io/zipcoder/ParenCheckerTest.java @@ -5,4 +5,66 @@ public class ParenCheckerTest { + @Test + public void testCheckParenOne(){ + ParenChecker parenChecker = new ParenChecker(); + String str = "(This is a test) string with some (parenthesis)"; + + boolean expected = true; + boolean actual = parenChecker.checkParen(str); + Assert.assertEquals(expected, actual); + } + + + @Test + public void testCheckParenTwo(){ + ParenChecker parenChecker = new ParenChecker(); + String str = ")This is a test( string with some (parenthesis)"; + + boolean expected = false; + boolean actual = parenChecker.checkParen(str); + Assert.assertEquals(expected, actual); + } + + @Test + public void testCheckParenThree(){ + ParenChecker parenChecker = new ParenChecker(); + String str = "(This is( a test (string with some parenthesis)"; + + boolean expected = false; + boolean actual = parenChecker.checkParen(str); + Assert.assertEquals(expected, actual); + } + + @Test + public void testCheckParenFour(){ + ParenChecker parenChecker = new ParenChecker(); + String str = "]"; + + boolean expected = false; + boolean actual = parenChecker.checkParen(str); + Assert.assertEquals(expected, actual); + } + + @Test + public void testCheckParenFive(){ + ParenChecker parenChecker = new ParenChecker(); + String str = " <{[( )]} >"; + + boolean expected = true; + boolean actual = parenChecker.checkParen(str); + Assert.assertEquals(expected, actual); + } + + @Test + public void testCheckParenSix(){ + ParenChecker parenChecker = new ParenChecker(); + String str = "< This ' is a ' test \"String\" with some parenthesis>"; + + boolean expected = true; + boolean actual = parenChecker.checkParen(str); + Assert.assertEquals(expected, actual); + } + + } \ No newline at end of file diff --git a/src/test/java/io/zipcoder/WCTest.java b/src/test/java/io/zipcoder/WCTest.java index 895e831..52da0f5 100644 --- a/src/test/java/io/zipcoder/WCTest.java +++ b/src/test/java/io/zipcoder/WCTest.java @@ -5,7 +5,39 @@ import java.util.ArrayList; import java.util.Arrays; +import java.util.TreeMap; public class WCTest { -} \ No newline at end of file + @Test + public void testWordCounter() { + + WC testWC = new WC(WC.class.getResource("/someTextFile.txt").getFile()); + String expected = "{26=1, a=1, am=2, hello=1, i=2, is=1, keith=1, my=1, name=1, person=1}"; + String actual = testWC.wordCountStorage().toString(); + + Assert.assertEquals(expected, actual); + } + + @Test + public void testWordCounterTwo() { + + WC testWC = new WC(WC.class.getResource("/robertFrost.txt").getFile()); + String expected = "{two=2, roads=2, diverged=2, in=4, a=3, yellow=1, wood=2, and=9, sorry=1, i=9, could=2, not=1, travel=1, both=2, be=2, one=3, traveler=1, long=1, stood=1, looked=1, down=1, as=5, far=1, to=2, where=1, it=2, bent=1, the=8, undergrowth=1, then=1, took=2, other=1, just=1, fair=1, having=1, perhaps=1, better=1, claim=1, because=1, was=1, grassy=1, wanted=1, wear=1, though=1, for=2, that=3, passing=1, there=1, had=2, worn=1, them=1, really=1, about=1, same=1, morning=1, equally=1, lay=1, leaves=1, no=1, step=1, trodden=1, black=1, oh=1, kept=1, first=1, another=1, day=1, yet=1, knowing=1, how=1, way=2, leads=1, on=1, doubted=1, if=1, should=1, ever=1, come=1, back=1, shall=1, telling=1, this=1, with=1, sigh=1, somewhere=1, ages=2, hence=1, less=1, traveled=1, by=1, has=1, made=1, all=1, difference=1}"; + + String actual = testWC.wordCountStorage().toString(); + + Assert.assertEquals(expected, actual); + } + + @Test + public void testSortedWordCount(){ + WC testWC = new WC(WC.class.getResource("/someTextFile.txt").getFile()); + String expected = "{i=2, am=2, hello=1, my=1, name=1, is=1, keith=1, a=1, person=1, 26=1}"; + String actual = testWC.sortInDescOrderByValue().toString(); + + Assert.assertEquals(expected, actual); + + } + +}