Skip to content

Instantly share code, notes, and snippets.

@bentrevett
Last active February 5, 2022 11:50
Show Gist options
  • Save bentrevett/c69a66fe9536cdfc6044bc76882f2b55 to your computer and use it in GitHub Desktop.
Save bentrevett/c69a66fe9536cdfc6044bc76882f2b55 to your computer and use it in GitHub Desktop.
import re
def is_basic_expr(expr):
"""
A basic expression has one or more positive integers separated by +-*/.
"""
# finds more than one space
if re.search("\s\s+", expr):
return False
# finds negative numbers
if re.search("-\d+", expr):
return False
# make sure ends in digit
if not re.search("\d+$", expr):
return False
# make sure no double operators
if re.search("(\+|-|\*|\/)\s+(\+|-|\*|\/)", expr):
return False
# make sure no digits separated by space
if re.search("\d+\s\d+", expr):
return False
# make sure all characters are valid
return all([c in "0123456789+-*/ " for c in expr])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment