From 15257cd0728ddc613fdb6de53b14c53eb74bcc10 Mon Sep 17 00:00:00 2001 From: Leandro Espino Date: Wed, 30 Aug 2023 21:13:08 -0600 Subject: [PATCH] Reto #28 - python --- .../python/EspinoLeandroo.py" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 "Retos/Reto #28 - EXPRESI\303\223N MATEM\303\201TICA [Media]/python/EspinoLeandroo.py" diff --git "a/Retos/Reto #28 - EXPRESI\303\223N MATEM\303\201TICA [Media]/python/EspinoLeandroo.py" "b/Retos/Reto #28 - EXPRESI\303\223N MATEM\303\201TICA [Media]/python/EspinoLeandroo.py" new file mode 100644 index 0000000000..c77234e462 --- /dev/null +++ "b/Retos/Reto #28 - EXPRESI\303\223N MATEM\303\201TICA [Media]/python/EspinoLeandroo.py" @@ -0,0 +1,37 @@ +import re + +def main(): + expression1 = "5 + 6 / 7 - 4" + expression2 = "5 a 6" + + print(validate_math_expression(expression1)) + print(validate_math_expression(expression2)) + +def validate_math_expression(expression): + tokens = re.split(r'\s+', expression) + + if len(tokens) % 2 == 0: + return False + + for i, token in enumerate(tokens): + if i % 2 == 0: + if not is_number(token): + return False + else: + if not is_operation(token): + return False + + return True + +def is_number(token): + try: + float(token) + return True + except ValueError: + return False + +def is_operation(token): + return re.match(r'[+\-*/%]', token) + +if __name__ == "__main__": + main()