Skip to content

Finished parts 1, 2, 3. #23

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 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
works in simplified way only (assumes only brackets and no letters) -…
… will modify
  • Loading branch information
Luis Romero authored and Luis Romero committed Mar 13, 2018
commit 0c89c557cc0e3b3787d154f607a2df48430f8dec
145 changes: 143 additions & 2 deletions src/main/java/io/zipcoder/ParenChecker.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,152 @@
package io.zipcoder;

import java.util.Arrays;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.*;

public class ParenChecker {

private String inputExpression;
private Stack<String> stack;

public ParenChecker() {
this.stack = new Stack<String>();
}

public ParenChecker(String inputExpression) {
this.inputExpression = inputExpression;
this.stack = new Stack<String>();
}

public Stack<String> getStack() {
return stack;
}

public boolean isInputLengthEven(String input) {
boolean isInputLengthEven = true;
if (input.length() % 2 != 0) {
isInputLengthEven = false;
}
return isInputLengthEven;
}


public boolean isFirstCharCorrect(String input) {
boolean isFirstCharCorrect = true;
String incorrectFirstCharStringPattern = "^[)}\\]]";
Pattern incorrectFirstCharPattern = Pattern.compile(incorrectFirstCharStringPattern);
Matcher firstCharMatcher = incorrectFirstCharPattern.matcher(input);

if (firstCharMatcher.find()) {
isFirstCharCorrect = false;
}
return isFirstCharCorrect;
}

public boolean isLastCharCorrect(String input) {
boolean isLastCharCorrect = true;
String incorrectLastCharStringPattern = "[({\\[]$";

Pattern incorrectLastCharPattern = Pattern.compile(incorrectLastCharStringPattern);
Matcher lastCharMatcher = incorrectLastCharPattern.matcher(input);

if (lastCharMatcher.find()) {
isLastCharCorrect = false;
}
return isLastCharCorrect;
}

}
public boolean areAllParensPaired(String input) {
boolean areAllParensPaired = true;

String[] inputStringArray = input.split("");
this.stack.addAll(Arrays.asList(inputStringArray));

for (int i = 0; i < stack.size() - 1; i++) {
if (stack.get(i).equals("{")) {
if (stack.get(i + 1).equals("[") || stack.get(i + 1).equals("(")) {
continue;
}
if (stack.get(i + 1).equals("}")) {
stack.remove(i);
stack.remove(i);
if (i == 0) i--;
else i -= 2;
continue;
}
if (stack.get(i + 1).equals("]") || stack.get(i + 1).equals(")")) {
break;
}
// Removing any other characters doesn't work right now.
// else {
// stack.remove(i + 1);
// i--;
// continue;
// }
}

if (stack.get(i).equals("[")) {
if (stack.get(i + 1).equals("{") || stack.get(i + 1).equals("(")) {
continue;
}
if (stack.get(i + 1).equals("]")) {
stack.remove(i);
stack.remove(i);
if (i == 0) i--;
else i -= 2;
continue;
}
if (stack.get(i + 1).equals("}") || stack.get(i + 1).equals(")")) {
break;
}
}

if (stack.get(i).equals("(")) {
if (stack.get(i + 1).equals("{") || stack.get(i + 1).equals("[")) {
continue;
}
if (stack.get(i + 1).equals(")")) {
stack.remove(i);
stack.remove(i);
if (i == 0) i--;
else i -= 2;
continue;
}
if (stack.get(i + 1).equals("}") || stack.get(i + 1).equals("]")) {
break;
}
}
}

if (stack.size() != 0) {
areAllParensPaired = false;
}
return areAllParensPaired;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (String string : stack) {
sb.append(string);
}
return sb.toString();
}

public static void main(String[] args) {
ParenChecker parenChecker = new ParenChecker();

String inputExpression = "{}{}{}{{}}";
System.out.println("isInputLengthEven: " + parenChecker.isInputLengthEven(inputExpression));
System.out.println("isFirstCharCorrect: " + parenChecker.isFirstCharCorrect(inputExpression));
System.out.println("isLastCharCorrect: " + parenChecker.isLastCharCorrect(inputExpression));
System.out.println();
System.out.printf("%25s", "areAllParensPaired: " + parenChecker.areAllParensPaired(inputExpression));
System.out.println();
System.out.printf("%20s", "original string: ");
System.out.println(inputExpression);
System.out.printf("%23s", "final string: " + parenChecker.toString());

}
}
101 changes: 101 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,107 @@
import org.junit.Assert;
import org.junit.Test;

import org.junit.Assert;
import org.junit.Test;

public class ParenCheckerTest {

private ParenChecker pc;

@Test
public void isInputLengthEvenFalseTest() {
// Given
String oddLengthString = "(()";
boolean expectedResponse = false;
// When
pc = new ParenChecker();
boolean actualResponse = pc.isInputLengthEven(oddLengthString);
// Then
Assert.assertEquals(expectedResponse, actualResponse);
}

@Test
public void isInputLengthEvenTrueTest() {
// Given
String evenLengthString = ")(()";
boolean expectedResponse = true;
// When
pc = new ParenChecker();
boolean actualResponse = pc.isInputLengthEven(evenLengthString);
// Then
Assert.assertEquals(expectedResponse, actualResponse);
}

@Test
public void isFirstCharCorrectTrueTest() {
// Given
String input = "asdfasd(()";
boolean expectedFirstCharTrueResponse = true;
// When
pc = new ParenChecker();
boolean actualResponse = pc.isFirstCharCorrect(input);
// Then
Assert.assertEquals(expectedFirstCharTrueResponse, actualResponse);
}

@Test
public void isFirstCharCorrectFalseTest() {
// Given
String input = "}(()";
boolean expectedFirstCharFalseResponse = false;
// When
pc = new ParenChecker();
boolean actualResponse = pc.isFirstCharCorrect(input);
// Then
Assert.assertEquals(expectedFirstCharFalseResponse, actualResponse);
}

@Test
public void isLastCharCorrectTrueTest() {
// Given
String input = "(()}";
boolean expectedLastCharTrueResponse = true;
// When
pc = new ParenChecker();
boolean actualResponse = pc.isLastCharCorrect(input);
// Then
Assert.assertEquals(expectedLastCharTrueResponse, actualResponse);
}

@Test
public void isLastCharCorrectFalseTest() {
// Given
String input = "}((){";
boolean expectedLastCharFalseResponse = false;
// When
pc = new ParenChecker();
boolean actualResponse = pc.isLastCharCorrect(input);
// Then
Assert.assertEquals(expectedLastCharFalseResponse, actualResponse);
}

@Test
public void areAllParensPairedTrueTest() {
// Given
String input = "()()()";
boolean expectedAreAllParensPairedTrue = true;
// When
pc = new ParenChecker();
boolean actualAreAllParensPaired = pc.areAllParensPaired(input);
// Then
Assert.assertEquals(expectedAreAllParensPairedTrue, actualAreAllParensPaired);
}

@Test
public void areAllParensPairedFalseTest() {
// Given
String input = "(((";
boolean expectedAreAllParensPairedFalse = false;
// When
pc = new ParenChecker();
boolean actualAreAllParensPaired = pc.areAllParensPaired(input);
// Then
Assert.assertEquals(expectedAreAllParensPairedFalse, actualAreAllParensPaired);
}

}