Skip to content

Up to Part 2 #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/main/java/io/zipcoder/ParenChecker.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,84 @@
package io.zipcoder;

import java.util.Stack;

public class ParenChecker {


Stack<Character> 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

*/
16 changes: 16 additions & 0 deletions src/main/java/io/zipcoder/WC.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,19 @@ public WC(Iterator<String> 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.
*/
53 changes: 53 additions & 0 deletions src/test/java/io/zipcoder/ParenCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
// }
}