From 29b17274e15d88f311657bf02c97a96800b2425f Mon Sep 17 00:00:00 2001 From: Anthony Jordan Date: Mon, 12 Mar 2018 19:40:48 -0400 Subject: [PATCH 1/4] Finished part 1 and 2 --- src/main/java/io/zipcoder/ParenChecker.java | 101 ++++++++++++++++++ .../java/io/zipcoder/ParenCheckerTest.java | 33 +++++- 2 files changed, 133 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/zipcoder/ParenChecker.java b/src/main/java/io/zipcoder/ParenChecker.java index caee675..4d7d767 100644 --- a/src/main/java/io/zipcoder/ParenChecker.java +++ b/src/main/java/io/zipcoder/ParenChecker.java @@ -1,4 +1,105 @@ package io.zipcoder; +import com.sun.xml.internal.fastinfoset.util.CharArray; + +import java.util.Stack; + public class ParenChecker { + private Stack stackOfChars = new Stack(); + + public boolean parenChecker(String inputString) { + char[] arrayAsChar = inputString.toCharArray(); + for (int i = 0; i < arrayAsChar.length; i++) { + characterChecker(arrayAsChar[i]); + } + + if (stackOfChars.isEmpty()) { + return true; + } else { + return false; + } + } + + private void characterChecker(char inputChar) { + switch (inputChar) { + case '(': + stackOfChars.push(inputChar); + break; + case '{': + stackOfChars.push(inputChar); + break; + case '[': + stackOfChars.push(inputChar); + break; + case '<': + stackOfChars.push(inputChar); + break; + case ')': + if (!stackOfChars.empty()) { + if (stackOfChars.peek().equals('(')) { + stackOfChars.pop(); + } else { + stackOfChars.push(inputChar); + } + } else { + stackOfChars.push(inputChar); + } + break; + case '}': + if (!stackOfChars.empty()) { + if (stackOfChars.peek().equals('{')) { + stackOfChars.pop(); + } else { + stackOfChars.push(inputChar); + } + } else { + stackOfChars.push(inputChar); + } + break; + case ']': + if (!stackOfChars.empty()) { + if (stackOfChars.peek().equals('[')) { + stackOfChars.pop(); + } else { + stackOfChars.push(inputChar); + } + } else { + stackOfChars.push(inputChar); + } + break; + case '>': + if (!stackOfChars.empty()) { + if (stackOfChars.peek().equals('<')) { + stackOfChars.pop(); + } else { + stackOfChars.push(inputChar); + } + } else { + stackOfChars.push(inputChar); + } + break; + case '\"': + if (!stackOfChars.empty()) { + if (stackOfChars.peek().equals('\"')) { + stackOfChars.pop(); + } else { + stackOfChars.push(inputChar); + } + } else { + stackOfChars.push(inputChar); + } + break; + case '\'': + if (!stackOfChars.empty()) { + if (stackOfChars.peek().equals('\'')) { + stackOfChars.pop(); + } else { + stackOfChars.push(inputChar); + } + } else { + stackOfChars.push(inputChar); + } + break; + } + } } diff --git a/src/test/java/io/zipcoder/ParenCheckerTest.java b/src/test/java/io/zipcoder/ParenCheckerTest.java index 76aa3b6..4c6660a 100644 --- a/src/test/java/io/zipcoder/ParenCheckerTest.java +++ b/src/test/java/io/zipcoder/ParenCheckerTest.java @@ -3,6 +3,37 @@ import org.junit.Assert; import org.junit.Test; +import javax.xml.ws.RequestWrapper; + public class ParenCheckerTest { -} \ No newline at end of file + @Test + public void parenCheckerTest1(){ + ParenChecker testChecker = new ParenChecker(); + boolean actual = testChecker.parenChecker("()()<>\"\"[]{}\'\'"); + Assert.assertTrue(actual); + } + + @Test + public void parenCheckerTest2(){ + ParenChecker testChecker = new ParenChecker(); + boolean actual = testChecker.parenChecker("ufeuifbbifeiu><<()"); + Assert.assertFalse(actual); + } + + @Test + public void parenCheckerTest3(){ + ParenChecker testChecker = new ParenChecker(); + boolean actual = testChecker.parenChecker("\"(({}))\""); + Assert.assertTrue(actual); + } + + @Test + public void parenCheckerTest4(){ + ParenChecker testChecker = new ParenChecker(); + boolean actual = testChecker.parenChecker("fsf(kfbf{}jnje)<>\"dsdjhsjdhj\""); + Assert.assertTrue(actual); + } + +} + From 1a0431750b9eb227fdbff53ca8b585124c018d82 Mon Sep 17 00:00:00 2001 From: Anthony Jordan Date: Tue, 13 Mar 2018 13:02:59 -0400 Subject: [PATCH 2/4] Part 3 working --- src/main/java/io/zipcoder/WC.java | 32 +++++++++++++++-- src/main/resources/someTextFile.txt | 5 +++ src/test/java/io/zipcoder/WCTest.java | 51 ++++++++++++++++++++++++++- 3 files changed, 85 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/zipcoder/WC.java b/src/main/java/io/zipcoder/WC.java index babb68c..6e96e0f 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -2,11 +2,11 @@ import java.io.FileNotFoundException; import java.io.FileReader; -import java.util.Iterator; -import java.util.Scanner; +import java.util.*; public class WC { private Iterator si; + private TreeMap wordMap = new TreeMap(); public WC(String fileName) { try { @@ -20,4 +20,32 @@ public WC(String fileName) { public WC(Iterator si) { this.si = si; } + + public void wordCounter(){ + String[] words; + for(String fileLine = si.next(); true; fileLine = si.next()) { + words = fileLine.split(" "); + for (String word: words) { + if (!wordMap.containsKey(word.toLowerCase())){ + wordMap.put(word.toLowerCase(), 1); + } else { + wordMap.put(word.toLowerCase(), wordMap.get(word)+ 1); + } + } + if (!si.hasNext()){ + break; + } + } + printAllResults(); + } + + public TreeMap getWordMap() { + return wordMap; + } + + public void printAllResults(){ + for (String key: wordMap.keySet()) { + System.out.println(key + " " + wordMap.get(key)); + } + } } diff --git a/src/main/resources/someTextFile.txt b/src/main/resources/someTextFile.txt index e69de29..b864815 100644 --- a/src/main/resources/someTextFile.txt +++ b/src/main/resources/someTextFile.txt @@ -0,0 +1,5 @@ +case test +call +apple apple apple test +test +case sweep \ 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..b8c92f4 100644 --- a/src/test/java/io/zipcoder/WCTest.java +++ b/src/test/java/io/zipcoder/WCTest.java @@ -1,11 +1,60 @@ package io.zipcoder; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; public class WCTest { + ArrayList stringList = new ArrayList(); -} \ No newline at end of file + @Before + public void setUp(){ + stringList.add("test"); + stringList.add("apple"); + stringList.add("test"); + stringList.add("case"); + stringList.add("case"); + stringList.add("case"); + } + + @Test + public void wordCheckerTest1(){ + WC wcTest = new WC(stringList.iterator()); + wcTest.wordCounter(); + int actual = wcTest.getWordMap().get("apple"); + int expected = 1; + Assert.assertEquals(actual, expected); + } + + @Test + public void wordCheckerTest2(){ + WC wcTest = new WC(stringList.iterator()); + wcTest.wordCounter(); + int actual = wcTest.getWordMap().get("case"); + int expected = 3; + Assert.assertEquals(actual, expected); + } + + @Test + public void wordCheckerTest3(){ + ArrayList testArray2 = new ArrayList(); + testArray2.add("apple test test case case case"); + WC wcTest = new WC(testArray2.iterator()); + wcTest.wordCounter(); + int actual = wcTest.getWordMap().get("case"); + int expected = 3; + Assert.assertEquals(actual, expected); + } + + @Test + public void wordCheckerTest4(){ + WC wcTest = new WC(WC.class.getResource("/someTextFile.txt").getFile()); + wcTest.wordCounter(); + int actual = wcTest.getWordMap().get("test"); + int expected = 3; + Assert.assertEquals(actual,expected); + } +} From 2819e4287fc0347ed1f7609f7d1e20a0e5538bb4 Mon Sep 17 00:00:00 2001 From: Anthony Jordan Date: Tue, 13 Mar 2018 14:29:51 -0400 Subject: [PATCH 3/4] Finished part 3 and refactored part 2 --- pom.xml | 12 ++ src/main/java/io/zipcoder/ParenChecker.java | 132 ++++++++++++-------- src/main/java/io/zipcoder/WC.java | 41 ++++-- 3 files changed, 118 insertions(+), 67 deletions(-) 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 4d7d767..5f4c7ef 100644 --- a/src/main/java/io/zipcoder/ParenChecker.java +++ b/src/main/java/io/zipcoder/ParenChecker.java @@ -35,71 +35,95 @@ private void characterChecker(char inputChar) { stackOfChars.push(inputChar); break; case ')': - if (!stackOfChars.empty()) { - if (stackOfChars.peek().equals('(')) { - stackOfChars.pop(); - } else { - stackOfChars.push(inputChar); - } - } else { - stackOfChars.push(inputChar); - } + checkClosingParen(inputChar); break; case '}': - if (!stackOfChars.empty()) { - if (stackOfChars.peek().equals('{')) { - stackOfChars.pop(); - } else { - stackOfChars.push(inputChar); - } - } else { - stackOfChars.push(inputChar); - } + checkClosingBracket(inputChar); break; case ']': - if (!stackOfChars.empty()) { - if (stackOfChars.peek().equals('[')) { - stackOfChars.pop(); - } else { - stackOfChars.push(inputChar); - } - } else { - stackOfChars.push(inputChar); - } + checkClosingSquareBracket(inputChar); break; case '>': - if (!stackOfChars.empty()) { - if (stackOfChars.peek().equals('<')) { - stackOfChars.pop(); - } else { - stackOfChars.push(inputChar); - } - } else { - stackOfChars.push(inputChar); - } + checkClosingAngleBracket(inputChar); break; case '\"': - if (!stackOfChars.empty()) { - if (stackOfChars.peek().equals('\"')) { - stackOfChars.pop(); - } else { - stackOfChars.push(inputChar); - } - } else { - stackOfChars.push(inputChar); - } + checkDoubleQuotes(inputChar); break; case '\'': - if (!stackOfChars.empty()) { - if (stackOfChars.peek().equals('\'')) { - stackOfChars.pop(); - } else { - stackOfChars.push(inputChar); - } - } else { - stackOfChars.push(inputChar); - } + checkSingleQuotes(inputChar); break; } } + + private void checkSingleQuotes(char inputChar) { + if (!stackOfChars.empty()) { + if (stackOfChars.peek().equals('\'')) { + stackOfChars.pop(); + } else { + stackOfChars.push(inputChar); + } + } else { + stackOfChars.push(inputChar); + } + } + + private void checkDoubleQuotes(char inputChar) { + if (!stackOfChars.empty()) { + if (stackOfChars.peek().equals('\"')) { + stackOfChars.pop(); + } else { + stackOfChars.push(inputChar); + } + } else { + stackOfChars.push(inputChar); + } + } + + private void checkClosingAngleBracket(char inputChar) { + if (!stackOfChars.empty()) { + if (stackOfChars.peek().equals('<')) { + stackOfChars.pop(); + } else { + stackOfChars.push(inputChar); + } + } else { + stackOfChars.push(inputChar); + } + } + + private void checkClosingSquareBracket(char inputChar) { + if (!stackOfChars.empty()) { + if (stackOfChars.peek().equals('[')) { + stackOfChars.pop(); + } else { + stackOfChars.push(inputChar); + } + } else { + stackOfChars.push(inputChar); + } + } + + private void checkClosingBracket(char inputChar) { + if (!stackOfChars.empty()) { + if (stackOfChars.peek().equals('{')) { + stackOfChars.pop(); + } else { + stackOfChars.push(inputChar); + } + } else { + stackOfChars.push(inputChar); + } + } + + private void checkClosingParen(char inputChar) { + if (!stackOfChars.empty()) { + if (stackOfChars.peek().equals('(')) { + stackOfChars.pop(); + } else { + stackOfChars.push(inputChar); + } + } else { + stackOfChars.push(inputChar); + } + } } diff --git a/src/main/java/io/zipcoder/WC.java b/src/main/java/io/zipcoder/WC.java index 6e96e0f..e258c00 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -8,6 +8,7 @@ public class WC { private Iterator si; private TreeMap wordMap = new TreeMap(); + public WC(String fileName) { try { this.si = new Scanner(new FileReader(fileName)); @@ -22,30 +23,44 @@ public WC(Iterator si) { } public void wordCounter(){ + splitIntoSingleWords(); + sortMap(); + } + + private void splitIntoSingleWords() { String[] words; for(String fileLine = si.next(); true; fileLine = si.next()) { words = fileLine.split(" "); - for (String word: words) { - if (!wordMap.containsKey(word.toLowerCase())){ - wordMap.put(word.toLowerCase(), 1); - } else { - wordMap.put(word.toLowerCase(), wordMap.get(word)+ 1); - } - } + placeWordIntoMap(words); if (!si.hasNext()){ break; } } - printAllResults(); } - public TreeMap getWordMap() { - return wordMap; + private void placeWordIntoMap(String[] words) { + for (String word: words) { + if (!wordMap.containsKey(word.toLowerCase())){ + wordMap.put(word.toLowerCase(), 1); + } else { + wordMap.put(word.toLowerCase(), wordMap.get(word)+ 1); + } + } + } + + private void sortMap(){ + ArrayList> listOfEntries = new ArrayList>(wordMap.entrySet()); + listOfEntries.sort((entry1, entry2) -> entry2.getValue().compareTo(entry1.getValue())); + printAllResults(listOfEntries); } - public void printAllResults(){ - for (String key: wordMap.keySet()) { - System.out.println(key + " " + wordMap.get(key)); + private void printAllResults(ArrayList> inputList){ + for (Map.Entry entry: inputList) { + System.out.println(entry); } } + + public TreeMap getWordMap() { + return wordMap; + } } From aeaef90eaa5152d569195818184b3b659d20cdbc Mon Sep 17 00:00:00 2001 From: Anthony Jordan Date: Tue, 13 Mar 2018 16:00:57 -0400 Subject: [PATCH 4/4] Added method to remove unused characters --- src/main/java/io/zipcoder/WC.java | 8 ++++++++ src/main/resources/someTextFile.txt | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/zipcoder/WC.java b/src/main/java/io/zipcoder/WC.java index e258c00..1cc79e6 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -31,6 +31,7 @@ private void splitIntoSingleWords() { String[] words; for(String fileLine = si.next(); true; fileLine = si.next()) { words = fileLine.split(" "); + words = stripUnwantedChars(words); placeWordIntoMap(words); if (!si.hasNext()){ break; @@ -60,6 +61,13 @@ private void printAllResults(ArrayList> inputList){ } } + public String[] stripUnwantedChars(String[] inputArray){ + for (int i = 0; i < inputArray.length; i++) { + inputArray[i]= inputArray[i].replaceAll("[^a-zA-Z']", ""); + } + return inputArray; + } + public TreeMap getWordMap() { return wordMap; } diff --git a/src/main/resources/someTextFile.txt b/src/main/resources/someTextFile.txt index b864815..048f5a1 100644 --- a/src/main/resources/someTextFile.txt +++ b/src/main/resources/someTextFile.txt @@ -2,4 +2,5 @@ case test call apple apple apple test test -case sweep \ No newline at end of file +case sweep +sweep! \ No newline at end of file