Commit 2fde9c32 authored by michael lundquist's avatar michael lundquist
Browse files

Adding the 5th assignment problem statement and a first crack at the 5th...

Adding the 5th assignment problem statement and a first crack at the 5th assignment method they want us to implement.
parent ce480fec
Pipeline #4086 failed with stage
in 1 minute and 20 seconds
package com.ds.fifthassignment;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
package com.ds.fifthassignment;
import java.util.Stack;
public class Application {
public class Bracket {
public static final char LEFT_P = '(';
public static final char RIGHT_P = ')';
public static final char LEFT_B = '[';
public static final char RIGHT_B = ']';
public static final char LEFT_CURL_B = '{';
public static final char RIGHT_CURL_B = '}';
public static final String LEFT = String(
[LEFT_P, LEFT_B, LEFT_CURL_B]);
public static final String RIGHT = String(
[RIGHT_P, RIGHT_B, RIGHT_CURL_B]);
}
public static void main(String[] args) {
char[] input1 = input1 = "[(4+5+1) x (4 / 2)]".toCharArray();
char[] input2 = "[(4+5+1) x (4 / 2]".toCharArray();
System.out.println(isValid(input1));
System.out.println(isValid(input2));
}
/**
* validates the brackets of a char[]
*
* @param input a char[] to validate
*/
public static boolean isValid(char[] input) {
Stack s = new Stack();
for (char c: input){
if( Bracket.RIGHT.indexOf(c) != -1 ){
//right (closing) bracket
if(s.isEmpty() || !s.pop().equals(getPair(c))){
return false;
}
}else if(Bracket.LEFT.indexOf(c) != -1){
//left (opening) bracket
s.push(c);
}
}
if(!s.isEmpty()){
return false;
}
return true;
}
public static char getPair(char symbol) {
if(symbol == Bracket.LEFT_B) return Bracket.RIGHT_B;
if(symbol == Bracket.RIGHT_B) return Bracket.LEFT_B;
if(symbol == Bracket.RIGHT_P) return Bracket.LEFT_P;
if(symbol == Bracket.LEFT_P) return Bracket.RIGHT_P;
if(symbol == Bracket.LEFT_CURL_B) return Bracket.RIGHT_CURL_B;
if(symbol == Bracket.RIGHT_CURL_B) return Bracket.LEFT_CURL_B;
else return '0';
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment