Math Expression Evaluator

PythonPythonIntermediate
Practice Now

Introduction

In this programming challenge, you will implement two functions that evaluate mathematical expressions. The first function, evaluate_expression, evaluates a string arithmetic expression consisting of +,-,*,/,%,** and () operators. The second function, advanced_evaluate_expression, evaluates a mathematical expression consisting of various mathematical functions and constants. Both functions use the shunting yard algorithm and stack to convert the input expression into postfix notation (also known as Reverse Polish Notation), then evaluate it using a stack.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) python(("`Python`")) -.-> python/AdvancedTopicsGroup(["`Advanced Topics`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/BasicConceptsGroup -.-> python/strings("`Strings`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/ControlFlowGroup -.-> python/while_loops("`While Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/ErrorandExceptionHandlingGroup -.-> python/catching_exceptions("`Catching Exceptions`") python/ErrorandExceptionHandlingGroup -.-> python/raising_exceptions("`Raising Exceptions`") python/AdvancedTopicsGroup -.-> python/regular_expressions("`Regular Expressions`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/BasicConceptsGroup -.-> python/python_shell("`Python Shell`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-8739{{"`Math Expression Evaluator`"}} python/variables_data_types -.-> lab-8739{{"`Math Expression Evaluator`"}} python/numeric_types -.-> lab-8739{{"`Math Expression Evaluator`"}} python/strings -.-> lab-8739{{"`Math Expression Evaluator`"}} python/type_conversion -.-> lab-8739{{"`Math Expression Evaluator`"}} python/conditional_statements -.-> lab-8739{{"`Math Expression Evaluator`"}} python/for_loops -.-> lab-8739{{"`Math Expression Evaluator`"}} python/while_loops -.-> lab-8739{{"`Math Expression Evaluator`"}} python/lists -.-> lab-8739{{"`Math Expression Evaluator`"}} python/tuples -.-> lab-8739{{"`Math Expression Evaluator`"}} python/dictionaries -.-> lab-8739{{"`Math Expression Evaluator`"}} python/function_definition -.-> lab-8739{{"`Math Expression Evaluator`"}} python/importing_modules -.-> lab-8739{{"`Math Expression Evaluator`"}} python/standard_libraries -.-> lab-8739{{"`Math Expression Evaluator`"}} python/catching_exceptions -.-> lab-8739{{"`Math Expression Evaluator`"}} python/raising_exceptions -.-> lab-8739{{"`Math Expression Evaluator`"}} python/regular_expressions -.-> lab-8739{{"`Math Expression Evaluator`"}} python/math_random -.-> lab-8739{{"`Math Expression Evaluator`"}} python/python_shell -.-> lab-8739{{"`Math Expression Evaluator`"}} python/build_in_functions -.-> lab-8739{{"`Math Expression Evaluator`"}} end

Math Expression Evaluator

In this challenge, you need to complete an evaluate_expression function that evaluates a string arithmetic expression, and follow the requirements below.

Requirements

Please complete the evaluate_expression function in the file /home/labex/project/basic_functionality.py.

Your task is to create a Python function that can evaluate a string expression consisting of arithmetic operators (+, -, *, /) and parentheses. The function should follow the standard order of operations (PEMDAS) and return the result of the expression.

The function should have the following signature:

Constraints

  • The input expression will contain only digits, parentheses, and the four basic arithmetic operators: +, -, *, and /.
  • The input expression will be a valid arithmetic expression.
  • The input expression may contain spaces, but these should be ignored.
  • The input expression will have no more than 10000 characters.
  • Implement support for the modulo operator (%) and the power operator (**).
  • You cannot use the eval function.

Example

>>> evaluate_expression("2 + 3 * 4")
14
>>> evaluate_expression("(2 + 3) * 4")
20
>>> evaluate_expression("3 + 4 * 2 / ( 1 - 5 )")
5.5
>>> evaluate_expression("2 ** 3 % 3")
2
>>> evaluate_expression("2 ** (3 % 2)")
2
>>> evaluate_expression("10 % 3 * 2")
2

Advanced Math Expression Evaluator

In this challenge, you need to complete a more advanced function called advanced_evaluate_expression, which adds some new requirements compared to the evaluate_expressionh function.

Requirements

Please complete the advanced_evaluate_expression function in the file /home/labex/project/advanced_functionality.py.

Sure, here's an enhanced version of the evaluate_expression function that supports the following additional features:

  • On the basis of the evaluate_expression function, the effect that has been achieved before is still guaranteed to be achieved, with the following new conditions added on top of it.
  • Unary minus: You can use the - operator as a unary operator to negate a number.
  • Scientific notation: You can use the e or E character to specify a number in scientific notation (e.g., 1.23e4 is equivalent to 1.23 * 10 ** 4).
  • Constants: You can use the pi and e constants in your expressions.
  • Functions: You can use the sin, cos, and tan functions in your expressions.
  • You still can't use the eval function.

Summary

Congratulations, great job! You have successfully completed the programming challenge of implementing two functions to evaluate mathematical expressions. Your implementation of evaluate_expression and advanced_evaluate_expression shows a clear understanding of the shunting yard algorithm and stack-based evaluation of postfix notation. You've also done an excellent job of incorporating error handling for invalid input expressions and attempted division or modulo by zero errors in both functions. Keep up the good work, and continue to develop your skills in Python and computer science!

Other Python Tutorials you may like