← Back to challenges

Check if a String is a Mathematical Expression

PythonHardregexvalidationstrings

Instructions

Create a function that takes an input (e.g. "5 + 4") and returns True if it's a mathematical expression or False if not.

Examples

math_expr("4 + 5") ➞ True

math_expr("4*6") ➞ True

math_expr("4*no") ➞ False

Notes

  • Should only work with the following operations: +, -, *, /, %
  • You don't need to test for floats.
  • int1 and int2 will only be from 0-9.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Basic Statistics: Median