diff --git a/src/main/java/io/zipcoder/ParenChecker.java b/src/main/java/io/zipcoder/ParenChecker.java index caee675..ffb48bd 100644 --- a/src/main/java/io/zipcoder/ParenChecker.java +++ b/src/main/java/io/zipcoder/ParenChecker.java @@ -1,4 +1,84 @@ package io.zipcoder; +import java.util.Stack; + public class ParenChecker { + + + Stack stackOfParentheses = new Stack(); + + // Part 1 - gonna pass in the chars we are looking for contained within a string ' ' + public boolean checkParentheses(String string) { + + for (int i = 0; i < string.length(); i++) { + if (string.charAt(i) == ')' && stackOfParentheses.empty()) { + break; + } + if (string.charAt(i) == '(') { + stackOfParentheses.push(string.charAt(i)); // push - element on top of stack + } + if (string.charAt(i) == ')') { + stackOfParentheses.pop(); // pop - removes & returns the top element of stack + } + } + return stackOfParentheses.empty(); // true if nothing is on top of stack + } + + + // Part 2 + public boolean openAndClosedCharacters(String string) { + + for (int i = 0; i < string.length(); i++) { + char currentChar = string.charAt(i); + + // checking opening characters + if (currentChar == '(' || currentChar == '{' + || currentChar == '[' || currentChar == '<' + || currentChar == '"' || currentChar == '\'') { + stackOfParentheses.push(currentChar); + } + // checking closing characters + if (currentChar == ')' || currentChar == '}' + || currentChar == ']' || currentChar == '>' + || currentChar == '"' || currentChar == '\'') { + + if (stackOfParentheses.isEmpty()) { + return false; + } + // checking both opening and closing characters + char lastChar = stackOfParentheses.peek(); // peek- returns element on top of stack w/o removing it + if (currentChar == ')' && lastChar == '(' + || currentChar == '}' && lastChar == '{' + || currentChar == ']' && lastChar == '[' + || currentChar == '>' && lastChar == '<' + || currentChar == '"' && lastChar == '"' + || currentChar == '\'' && lastChar == '\'') { + stackOfParentheses.pop(); + } + } + } + + return stackOfParentheses.isEmpty(); + } } + + +/* +TC-Collections + + Paren Checker + + Part 1: + Create a class with a method that verifies all parens () are paired. + - HINT: Use a stack. + - Boolean + - + + Part 2: + Now create a method that checks that all opening characters have a closing one. + + Characters include () {} [] <> "" '' + + - Boolean + +*/ \ 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..4c503a0 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -21,3 +21,19 @@ public WC(Iterator si) { this.si = si; } } + +/* +WC + +Write a program that counts all of the words in a file and +prints out all of the words and their counts in descending order. + +You can put an text file in resources and get that string's filename from + +WC.class.getResource("/filename").getFile() + +I would suggest grabbing a book from gutenberg.org and use that as a testing set. + +Also, there is a String iterator constructor so +that you can also write some tests using some data built within the tests. + */ \ 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..fb9ac8f 100644 --- a/src/test/java/io/zipcoder/ParenCheckerTest.java +++ b/src/test/java/io/zipcoder/ParenCheckerTest.java @@ -5,4 +5,57 @@ public class ParenCheckerTest { + +// @Test +// public void checkParenthesesTest1() { +// // Given +// ParenChecker testParentheses = new ParenChecker(); +// +// // When +// String string1 = "Eagles are (Super Bowl) Champs"; +// boolean actual = testParentheses.checkParentheses(string1); +// +// // Then +// Assert.assertTrue(actual); +// } +// +// +// @Test +// public void checkParenthesesTest2() { +// // Given +// ParenChecker testParentheses = new ParenChecker(); +// +// // When +// String string2 = "Cowboys will not be (Super Bowl Champs anytime soon"; +// boolean actual = testParentheses.checkParentheses(string2); +// +// // Then +// Assert.assertFalse(actual); +// } +// +// @Test +// public void openAndClosedCharactersTest1() { +// // Given +// ParenChecker testParentheses = new ParenChecker(); +// +// // When +// String string1 = "Eagles will [repeat] as (Super Bowl) Champs"; +// boolean actual = testParentheses.openAndClosedCharacters(string1); +// +// // Then +// Assert.assertTrue(actual); +// } +// +// @Test +// public void openAndClosedCharactersTest2() { +// // Given +// ParenChecker testParentheses = new ParenChecker(); +// +// // When +// String string2 = "[Cowboys] will not be (Super Bowl Champs> {anytime soon"; +// boolean actual = testParentheses.openAndClosedCharacters(string2); +// +// // Then +// Assert.assertFalse(actual); +// } } \ No newline at end of file