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

In Python Define a recursive function that finds the result of an arithmetic str

ID: 3935372 • Letter: I

Question

In Python

Define a recursive function that finds the result of an arithmetic string. The given string will contain numbers, operators (+, -, *, /), and possibly spaces. You should compute the result respecting order of operations. The function should return an int if the string contains only whole numbers and does not use division. Otherwise, the function should return a float. You do not need to consider division by zero in your solution.

Parameter(s): 1. A string containing numbers, operators (+, -, *, /), and possibly spaces.

Return Value: An int or a float of the resulting value.

Example(s): >>> print(solve_eq("1 + 1")) 2

>>> print(solve_eq("1 + 5 * 4")) 21

>>> print(solve_eq("1.0 + 5 * 4")) 21.0

>>> print(solve_eq("1 - 10 * 5 / 80")) 0.375

Explanation / Answer

Following is the required code in python :

def solve_eq( inp ):
   inp = ''.join( inp.split() );
   breakInp = [];
   z = '';
   finalAnswerIsInt = True;
   for i in range(len(inp)):
       if inp[i] == '/' or inp[i] == '.':
           finalAnswerIsInt = False;
       if inp[i] in ['+','-','*','/']:      
           breakInp.append(z);
           breakInp.append( inp[i] );
           z = '';
       else:
           z = z + inp[i];
   breakInp.append(z);

   #deal with / first
   i = 0;
   z = len(breakInp);
   while( i < z ):
       if breakInp[i] == '/':
           breakInp[i-1] = float(breakInp[i-1]) / float(breakInp[i+1]);
           breakInp = breakInp[:i] + breakInp[i+2:];
           z = z - 2;
       else:
           i = i + 1;
  
   #deal with * first
   i = 0;
   z = len(breakInp);
   while( i < z ):
       if breakInp[i] == '*':
           breakInp[i-1] = float(breakInp[i-1]) * float(breakInp[i+1]);
           breakInp = breakInp[:i] + breakInp[i+2:];
           z = z - 2;
       else:
           i = i + 1;

   #deal with - first
   i = 0;
   z = len(breakInp);
   while( i < z ):
       if breakInp[i] == '-':
           breakInp[i-1] = float(breakInp[i-1]) - float(breakInp[i+1]);
           breakInp = breakInp[:i] + breakInp[i+2:];
           z = z - 2;
       else:
           i = i + 1;

   #deal with + first
   i = 0;
   z = len(breakInp);
   while( i < z ):
       if breakInp[i] == '+':
           breakInp[i-1] = float(breakInp[i-1]) + float(breakInp[i+1]);
           breakInp = breakInp[:i] + breakInp[i+2:];
           z = z - 2;
       else:
           i = i + 1;

   if finalAnswerIsInt:
       breakInp[0] = int(breakInp[0]);
   return breakInp[0];

print(solve_eq("1 + 1"));
print(solve_eq("1 + 5 * 4"));
print(solve_eq("1.0 + 5 * 4"));
print(solve_eq("1 - 10 * 5 / 80"));

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