In Python Define a recursive function that finds the result

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

Solution

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\"));

In Python Define a recursive function that finds the result of an arithmetic string. The given string will contain numbers, operators (+, -, *, /), and possibly
In Python Define a recursive function that finds the result of an arithmetic string. The given string will contain numbers, operators (+, -, *, /), and possibly

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site