Skip to content

Part 1, 2 & Tests Passing #14

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
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
<groupId>io.zipcoder</groupId>
<artifactId>collections</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
Expand Down
96 changes: 96 additions & 0 deletions src/main/java/io/zipcoder/ParenChecker.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,100 @@
package io.zipcoder;

//import java.util.HashMap;
//import java.util.Map;
import java.util.Stack;

public class ParenChecker {

Stack<Character> parenStack = new Stack();

//going to pass in the chars we are looking for contained within a String ''
public boolean parenChecker(String string) {
for (int i = 0; i < string.length(); i++) {
char charCurrent = string.charAt(i);
if (charCurrent == '(') {
parenStack.push(charCurrent);
}
if (charCurrent == ')') { //if stack's empty, no existing pair for it to link to.
if (parenStack.isEmpty())
return false;

char charOnStack = parenStack.peek();
if (charCurrent == ')' && charOnStack == '(') {
parenStack.pop();
} else
return false;
}
}
return parenStack.empty();
}

public boolean pairOfParenthesies(String string) {
Stack<Character> parenStack = new Stack();
for (int i =0; i < string.length(); i++) {
char currentCharacter = string.charAt(i);
if (currentCharacter == '[' || currentCharacter == '"' || currentCharacter == '<' || currentCharacter == '\'' || currentCharacter == '{' || currentCharacter == '(') {
parenStack.push(currentCharacter);
}
if (currentCharacter == ']' || currentCharacter == '"' || currentCharacter == '>' || currentCharacter == '\'' || currentCharacter == '}' || currentCharacter == ')' ) {

if (parenStack.isEmpty())
return false;
// look at the object at the top of the stack but don't remove, PEEK
char finalCharacter = parenStack.peek();
if (currentCharacter == ']' && finalCharacter == '[' || currentCharacter == '"' && finalCharacter == '"'
|| currentCharacter == '>' && finalCharacter =='<' || currentCharacter == '\'' && finalCharacter == '\'' || currentCharacter == '}' && finalCharacter == '{' || currentCharacter == ')' && finalCharacter == '(') {
parenStack.pop();
} else
return false;
}
}
return parenStack.isEmpty();
}
}

//check if characters are balanced//
//
// private static boolean isBalanced(String str) {
// Map<Character, Character> bracketPairs = new HashMap<Character, Character>(){
// {
// put('(', ')');
// put('{', '}');
// put('[', ']');
// put('<', '>');
// }
// };
//
// //in order to be balanced, there ought to be an even number of chars
// if(str.length() % 2 !=0) {
// return false;
// }
// //stores opening braces in this case
// Stack<Character> halfBraces = new Stack<>();
//
// for(int i = 0; i < str.length(); i++) {
// //if characters (bracket) is opening, then store into stack
// if(bracketPairs.containsKey(str.charAt(i))){
// halfBraces.push(str.charAt(i));
// }
//
// //if closing bracket is not equal to top of stack or if stack is empty, return false
// else if(halfBraces.isEmpty() || bracketPairs.get(halfBraces.pop()) !=str.charAt(i)) {
// return false;
// }
// }
// return halfBraces.isEmpty() ? true : false;
// }
//
//}


// public static boolean checkParenthesies(String input) {
// Stack<Character> stack = new Stack<Character>();
// for (int i = 0; i < input.length(); i++) {
// char c = input.charAt(i);
// if (c == '[' || c == '(' || c == '{') {
// stack.push(c);
// }
// }
// }
108 changes: 108 additions & 0 deletions src/test/java/io/zipcoder/ParenCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,114 @@
import org.junit.Assert;
import org.junit.Test;

import java.util.Stack;


public class ParenCheckerTest {

@Test
public void parenthesisCheckerTest() {
//Given
ParenChecker parenthesisTest = new ParenChecker();

//When
String testString = "()";
boolean actual = parenthesisTest.parenChecker(testString);

//Then
Assert.assertTrue(actual);
}

@Test
public void parenthesisCheckerTest2() {
//Given
ParenChecker parenthesisTest = new ParenChecker();

//When
String testString2 = "()(";
boolean actual = parenthesisTest.parenChecker(testString2);

//Then
Assert.assertFalse(actual);
}
@Test
public void parenthesisCheckerTest3() {
//Given
ParenChecker parenthesisTest = new ParenChecker();

//When
String testString3 = "(i)p(";
boolean actual = parenthesisTest.parenChecker(testString3);

//Then
Assert.assertFalse(actual);
}

@Test
public void parenthesisCheckerTest4() {
//Given
ParenChecker parenthesisTest = new ParenChecker();

//When
String testString4 = "a(ki)loop(jj)hi";
boolean actual = parenthesisTest.parenChecker(testString4);

//Then
Assert.assertTrue(actual);
}


@Test
public void pairCheckerTest1() {
//Given
ParenChecker parenthesisTest = new ParenChecker();


//When
String testString2 = ")()(";
boolean actual = parenthesisTest.pairOfParenthesies(testString2);

//Then
Assert.assertFalse(actual);
}

@Test
public void pairCheckerTest2() {
//Given
ParenChecker parenthesisTest = new ParenChecker();

//When
String testString = "[]<>";
boolean actual = parenthesisTest.pairOfParenthesies(testString);

//Then
Assert.assertTrue(actual);
}

@Test
public void pairCheckerTest3() {
//Given
ParenChecker parenthesisTest = new ParenChecker();


//When
String testString3 = "atom)p(min)low(da";
boolean actual = parenthesisTest.pairOfParenthesies(testString3);

//Then
Assert.assertFalse(actual);
}

@Test
public void pairCheckerTest4() {
//Given
ParenChecker parenthesisTest = new ParenChecker();

//When
String testString4 = "do[re]mi<fig>aro";
boolean actual = parenthesisTest.pairOfParenthesies(testString4);

//Then
Assert.assertTrue(actual);
}
}