diff --git a/src/main/java/io/zipcoder/ParenChecker.java b/src/main/java/io/zipcoder/ParenChecker.java index caee675..0cf27e1 100644 --- a/src/main/java/io/zipcoder/ParenChecker.java +++ b/src/main/java/io/zipcoder/ParenChecker.java @@ -1,4 +1,62 @@ package io.zipcoder; +import java.util.HashMap; +import java.util.Map; +import java.util.Stack; + public class ParenChecker { + //Create a class with a method that verifies all parens () are paired. HINT: Use a stack. + private Stack theStack; + private char charAt; + + + public ParenChecker() { + this.theStack = new Stack(); + } + + public boolean verifyParens(String theString) { + // char[] theChar = theString.toCharArray(); + char parOpen = '('; + char parClosed = ')'; + for (int i = 0; i < theString.length(); i++) { + charAt = theString.charAt(i); + if (theStack.contains(parOpen) && charAt == parClosed) { + theStack.pop(); + } else if (charAt == parOpen || charAt == parClosed) { + theStack.push(charAt); + } + } + + return theStack.empty(); + } + + + public boolean verifyAllOpensHaveEnds(String theString) { + boolean allOpensHavePairs = false; + theStack.clear(); + Map bracketPairs = new HashMap(); + bracketPairs.put('(', ')'); + bracketPairs.put('{', '}'); + bracketPairs.put('[', ']'); + bracketPairs.put('<', '>'); + bracketPairs.put('"', '"'); + bracketPairs.put('\'', '\''); + + for (int i = 0; i < theString.length(); i++) { + char theChar = theString.charAt(i); + if (bracketPairs.containsKey(theChar)) { + theStack.push(theChar); + } + + //how to account all things in between the open brackets. + //theStackisempty; + + else if (bracketPairs.get(theStack.peek()) == theChar){ + theStack.pop(); + } + } + return theStack.isEmpty(); + } } + + diff --git a/src/main/java/io/zipcoder/WC.java b/src/main/java/io/zipcoder/WC.java index babb68c..e3f0cdd 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 Map words; public WC(String fileName) { try { @@ -17,7 +17,57 @@ public WC(String fileName) { } } + public static void main(String[] args) { + WC theWC = new WC("src/main/resources/someTextFile.txt"); + theWC.readsStringFromfileAddstoMap(); + theWC.print(); + } + public WC(Iterator si) { this.si = si; } + + public void readsStringFromfileAddstoMap() { + words = new TreeMap(Collections.reverseOrder()); + while (this.si.hasNext()) { + String[] wordsArray = this.si.next().split("[^\\w]+"); + putsStringKeyInMap(wordsArray); + } + + } + + public void putsStringKeyInMap(String[] theWords) { + for (int i = 0; i < theWords.length; i++) { + String word = theWords[i]; + increaseValueOfWord(word); + } + } + + public void increaseValueOfWord(String theWord) { + int count = 1; + if (words.containsKey(theWord)) { + count = getCount(theWord); + words.put(theWord, count + 1); + } else { + words.put(theWord, count); + } + } + + public int getCount(String theWord) { + return words.get(theWord); + } + +// public void reverseOrderOfMap(){ +// Map newMap = new TreeMap(Collections.reverseOrder()); +// newMap.putAll(words); +// } + + + public void print() { + // need to print in decending order. + for (Map.Entry entry : words.entrySet()) + System.out.println(entry.getValue() + " : " + entry.getKey()); + } + } + diff --git a/src/main/resources/someTextFile.txt b/src/main/resources/someTextFile.txt index e69de29..5dafeaf 100644 --- a/src/main/resources/someTextFile.txt +++ b/src/main/resources/someTextFile.txt @@ -0,0 +1,3 @@ +Yo My name is Joshua. +theNextPerson Yo,,,,, My name is Joshua.&*&(%^&*#$%^#$%@^$%&^&(%^*(%%$^#@$#$&^%$#(&*^%$#@99 +the next word in the book will be the next person who tells the next thing that akes the word that goes \ No newline at end of file diff --git a/src/test/java/io/zipcoder/AllOpensHaveEndsTest.java b/src/test/java/io/zipcoder/AllOpensHaveEndsTest.java new file mode 100644 index 0000000..edfe2b8 --- /dev/null +++ b/src/test/java/io/zipcoder/AllOpensHaveEndsTest.java @@ -0,0 +1,92 @@ +package io.zipcoder; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class AllOpensHaveEndsTest { + private ParenChecker theCheck; + @Before + public void setUp() throws Exception { + theCheck = new ParenChecker(); + } + + @Test + public void verifyAllOpensHaveEndsTest() { + String testString = "()"; + boolean expect = true; + boolean actual = theCheck.verifyAllOpensHaveEnds(testString); + + Assert.assertEquals(expect, actual); + } + @Test + public void verifyAllOpensHaveEnds1Test() { + String testString = "()(/////////////////////////////////)"; + boolean expect = true; + boolean actual = theCheck.verifyAllOpensHaveEnds(testString); + + Assert.assertEquals(expect, actual); + } + + @Test + public void verifyAllOpensHaveEndsFalseTEst() { + String testString = "(("; + boolean expect = false; + boolean actual = theCheck.verifyAllOpensHaveEnds(testString); + + Assert.assertEquals(expect, actual); + } + + @Test + public void verifyAllOpensHaveEndsFalse1Test() { + String testString = "((/////////////////////////////)"; + boolean expect = false; + boolean actual = theCheck.verifyAllOpensHaveEnds(testString); + + Assert.assertEquals(expect, actual); + } + + + + @Test + public void verifyAllOpensHaveEndsFalse2Test() { + String testString = "((/////////////////////////////)}}}}}}}}}}}}}}}}}}}}}}}}{{{{{{{{{{{{{{{}}}"; + boolean expect = false; + boolean actual = theCheck.verifyAllOpensHaveEnds(testString); + + Assert.assertEquals(expect, actual); + } + + @Test + public void verifyAllOpensHaveEndsTrueTest() { + String testString = "((/////////////////////////////))<>"; + boolean expect = true; + boolean actual = theCheck.verifyAllOpensHaveEnds(testString); + + Assert.assertEquals(expect, actual); + } + + @Test + public void verifyAllOpensHaveEndsfalseTest() { + String testString = "((/////////////////////////////))<"; + boolean expect = false; + boolean actual = theCheck.verifyAllOpensHaveEnds(testString); + + Assert.assertEquals(expect, actual); + } + @Test + public void verifyAllOpensHaveEnds2Test() { + String testString = "sfsdfkjdsljfl"; + boolean expect = true; + boolean actual = theCheck.verifyAllOpensHaveEnds(testString); + + Assert.assertEquals(expect, actual); + } + + + + + +} \ 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..61f5b99 100644 --- a/src/test/java/io/zipcoder/ParenCheckerTest.java +++ b/src/test/java/io/zipcoder/ParenCheckerTest.java @@ -1,8 +1,81 @@ package io.zipcoder; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; +import java.util.Stack; + public class ParenCheckerTest { + private ParenChecker checker; + private String[] theString; + @Before + public void setUp() throws Exception { + checker = new ParenChecker(); + theString = new String[]{"the (yo)", "the(,()", "the)", "the))", "the()()(", "the)()(", "()()()*()"}; + } + + @Test + public void parenClosedCheckerTest(){ + String testString = theString[0]; + boolean expected = true; + boolean actual = checker.verifyParens(testString); + Assert.assertEquals(expected, actual); + } + + @Test + public void parenOneOpenCheckerTest(){ + String testString = theString[1]; + boolean expected = false; + boolean actual = checker.verifyParens(testString); + Assert.assertEquals(expected, actual); + } + + @Test + public void parenOneClosedCheckerTest(){ + String testString = theString[2]; + boolean expected = false; + boolean actual = checker.verifyParens(testString); + Assert.assertEquals(expected, actual); + } + + @Test + public void parenTwoClosedCheckerTest(){ + String testString = theString[3]; + boolean expected = false; + boolean actual = checker.verifyParens(testString); + Assert.assertEquals(expected, actual); + } + + @Test + public void parenTwClosedOneOpenCheckerTest(){ + String testString = theString[4]; + boolean expected = false; + boolean actual = checker.verifyParens(testString); + Assert.assertEquals(expected, actual); + } + + @Test + public void parenOneClosedTwoOpenCheckerTest(){ + String testString = theString[5]; + boolean expected = false; + boolean actual = checker.verifyParens(testString); + Assert.assertEquals(expected, actual); + } + + @Test + public void parenFourClosedCheckerTest(){ + String testString = theString[6]; + boolean expected = true; + boolean actual = checker.verifyParens(testString); + Assert.assertEquals(expected, actual); + } + + + + + + + } \ No newline at end of file