Last active
February 5, 2022 11:50
-
-
Save bentrevett/c69a66fe9536cdfc6044bc76882f2b55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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