HackerRank : Moody’s Analytics University Hackathon – Asterisk Expressions

By | September 30, 2016

In Mathematics, exponentiation has higher operator precedence than multiplication. This means that any exponentiation in the expression must be evaluated first before before any multiplication. For the purposes of this challenge, exponentiation is left-associative, which is different than you would normally evaluate it. This means that is evaluated as and not as .

In Python, the exponentiation operator is denoted by a double asterisk (**). For example, is expressed as a**k. To express multiplication, we use a single asterisk operator, *. For example, we express as a*b.

An expression, , consisting of decimal digits and asterisks is valid if and only if it forms a valid mathematical expression when each double-asterisk (i.e., **) is replaced with a math exponentiation sign. For example,2**4*5**3 is a valid expression translating to , while *2**3, 2***3, 4*5**, and 4**2* are invalid as they do not translate to valid mathematical expressions.

Given expressions consisting of decimal digits and asterisks, parse each expression and determine its validity. If an expression is valid, print its evaluated value modulo on a new line; if it’s invalid, print Syntax Errorinstead.

Input Format

The first line contains a single integer, , denoting the number of expressions.
Each line of the subsequent lines contains a single string, , denoting an expression.

Constraints

  • There are at most consecutive digits in an expression.
  • Consecutive sequences of digits in an expression will never start with .
  • Each expression consists of decimal digits (i.e., through ) and asterisks (*) only.

Output Format

For each expression, , print its answer on a new line. If is valid, the answer is the evaluated expression ; otherwise, it’s Syntax Error.

Sample Input

Sample Input 0

1
3*2**3**2*5

Sample Output 0

960

Explanation 0
We have expression to evaluate. Because exponentiation has higher operator precedence than multiplication, and because exponentiation is left-associative here, the expression evaluates to . Thus, we print on a new line.

Sample Input 1

1
3***4

Sample Output 1

Syntax Error

Explanation 1
We have expression to evaluate. Because this expression contains three consecutive asterisks, it cannot be evaluated as a mathematical expression. Thus, we print Syntax Error on a new line.

Solution

Only 1st and 2nd test cases are passing as of now. Trying to debug other corner cases and solve them.
If any suggestions, please suggest.



 public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */	Scanner scan= new Scanner(System.in);
		
		int n=scan.nextInt();
		scan.nextLine();
		
		for (int i = 0; i < n; i++) {
		String str= scan.nextLine();
		
		if(str.charAt(0)=='*' || str.contains("***") || str.charAt(str.length()-1)=='*')
			System.out.println("Syntax Error");
		
		else if( str.contains("*0*") || str.charAt(0)=='0' || str.charAt(str.length()-1)=='0')
		{
			System.out.println("0");
			break;
		}
		
		else
		{
			char a[]= str.toCharArray();
	
			Stack  st= new Stack();
			
			st.push(String.valueOf(str.charAt(0)));
			
			for(i=2;i<str.length();i++)
			{
				
				if(str.charAt(i)!='*' && str.charAt(i-1)=='*' && str.charAt(i-2)!='*' )
				{
					int initial=i;
				
				while(initial!=str.length()-1 )
					{

					if(str.charAt(initial+1)=='*')
						break;
					else
						initial++;	
					}
	
				String sub=str.substring(i,initial+1);
				i=initial;
					st.push(sub);
					
				}
				
				
				if(str.charAt(i)!='*'  && str.charAt(i-1)=='*' && str.charAt(i-2)=='*' )
				{
					int initial=i;
					
					while(initial!=str.length()-1 )
						{

						if(str.charAt(initial+1)=='*')
							break;
						else
							initial++;	
						}
		
					
					String sub=str.substring(i,initial+1);
					i=initial;

					int pop= Integer.parseInt(st.pop());
					int b= Integer.parseInt(sub);
					int mul=  (int) Math.pow(pop, b);
					st.push(String.valueOf(mul));					
				}
				
				if(str.charAt(i)=='*')
				{

				}

			}
			
			int f=1;
			for (String string : st) {
			f=f* Integer.parseInt(string);
			}
			System.out.println(f);
			
		}
	}
		
		
		
    }