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..5f4c7ef 100644 --- a/src/main/java/io/zipcoder/ParenChecker.java +++ b/src/main/java/io/zipcoder/ParenChecker.java @@ -1,4 +1,129 @@ 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 ')': + checkClosingParen(inputChar); + break; + case '}': + checkClosingBracket(inputChar); + break; + case ']': + checkClosingSquareBracket(inputChar); + break; + case '>': + checkClosingAngleBracket(inputChar); + break; + case '\"': + checkDoubleQuotes(inputChar); + break; + case '\'': + 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 babb68c..1cc79e6 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -2,11 +2,12 @@ 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 +21,54 @@ public WC(String fileName) { public WC(Iterator si) { this.si = si; } + + public void wordCounter(){ + splitIntoSingleWords(); + sortMap(); + } + + 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; + } + } + } + + 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); + } + + private void printAllResults(ArrayList> inputList){ + for (Map.Entry entry: inputList) { + System.out.println(entry); + } + } + + 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 e69de29..048f5a1 100644 --- a/src/main/resources/someTextFile.txt +++ b/src/main/resources/someTextFile.txt @@ -0,0 +1,6 @@ +case test +call +apple apple apple test +test +case sweep +sweep! \ 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..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); + } + +} + 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); + } +}