forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c228989
commit 3be3ae6
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
Retos/Reto #28 - EXPRESIÓN MATEMÁTICA [Media]/python/TiagoAlvarezSchiaffino.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
def check_math_expression(expression: str) -> bool: | ||
""" | ||
Check if a mathematical expression is correct. | ||
Args: | ||
expression (str): The mathematical expression to check. | ||
Returns: | ||
bool: True if the expression is correct, False otherwise. | ||
""" | ||
components = expression.split() | ||
|
||
if len(components) < 3 or len(components) % 2 == 0: | ||
return False | ||
|
||
for index, component in enumerate(components): | ||
if index % 2 == 0: | ||
try: | ||
float(component) | ||
except ValueError: | ||
return False | ||
else: | ||
if component not in ["+", "-", "*", "/", "%"]: | ||
return False | ||
|
||
return True | ||
|
||
if __name__ == "__main__": | ||
expression1 = "3 + 5" | ||
expression2 = "3 a 5" | ||
expression3 = "-3 + 5" | ||
expression4 = "- 3 + 5" | ||
expression5 = "-3 a 5" | ||
expression6 = "-3+5" | ||
|
||
print(check_math_expression(expression1)) | ||
print(check_math_expression(expression2)) | ||
print(check_math_expression(expression3)) | ||
print(check_math_expression(expression4)) | ||
print(check_math_expression(expression5)) | ||
print(check_math_expression(expression6)) |