Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given string data input representing a mathematical expression, determine the po

ID: 3880034 • Letter: G

Question

Given string data input representing a mathematical expression, determine the possible location(s) of the missing parenthesis. Although we show spaces between characters for readability, the input strings will have no spaces. All operands will be single digits.

Given ( 2 + 3 * 6 + 1. A right parenthesis is missing. It could be correctly placed in several locations:

( 2 + 3 ) * 6 + 1     Location 5

( 2 + 3 * 6 ) + 1     Location 7

( 2 + 3 * 6 + 1 )     Location 9

INPUT: There will be five lines of input. Each line will contain a string of characters with no spaces representing a mathematical expression. Each expression will have either a single left or right parenthesis. The operators used will be: +, , * and /.

OUTPUT: For each line of input, list all the locations in that expression where the missing left or right parenthesis can be correctly placed. Note: single digits are never enclosed.

SAMPLE INPUT                                                                  SAMPLE OUTPUT
1. ( 2 + 3 * 6 + 1                                                     1. 5, 7, 9
2. 2 – 5 * ( 6 + 1                                                   2. 9
3. 5 + 5 – 2 ) * 5                                                  3. 1, 3
4. 3 * 5 + ( 8 / 4 * 2                                              4. 9, 11
5. 2 + 8 / 4 * 5 )                                                    5. 1, 3, 5

TEST DATA

TEST INPUT                                                                TEST OUTPUT

1. 6 + 2 / 3 * 4 )                                                              1. 1, 3, 5

2. ( 2 – 2 + 2 + 3 * 4 / 2                                                   2. 5, 7, 9, 11, 13

3. 8 / ( 2 + 3 – 6 + 2                                                         3. 7, 9, 11

4. 7 – 5 + 8 * 6 / 2 ) + 1                                                  4. 1, 3, 5, 7

5. 9 + 6 ) / 2 – 4 + 5                                                        5. 1

Explanation / Answer

class MissingParenthesis{
public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  String input = sc.nextLine();  //scan input from the user
  input = input.replaceAll("\s", "");//remove all the spaces
  char[] arr = input.toCharArray();
  int index1 = input.indexOf("("); //store the indices of ( and ) in the expression as applicable
  int index2 = input.indexOf(")");
  if(index1>=0)      //check for the existance of ( in the expression
   for(int i=index1;i<arr.length;i++) {
    if(arr[i]=='+'||arr[i]=='–'||arr[i]=='*'||arr[i]=='/')
     System.out.println(i+3); //i represents the index of the operator 1 is added to compensate for the fact that array index starts from 0 not 1 and extra 2 is added to denote the location of the paranthesis
   }
  else
   for(int i=index2;i>0;i--) {
    if(arr[i]=='+'||arr[i]=='–'||arr[i]=='*'||arr[i]=='/')
     System.out.println(i); //same as above but since the loop is inverted, it does not need the corrections
   }
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote