Skip to content

Commit

Permalink
Reto mouredev#28 - Python
Browse files Browse the repository at this point in the history
  • Loading branch information
TiagoAlvarezSchiaffino committed Dec 22, 2023
1 parent c228989 commit 3be3ae6
Showing 1 changed file with 41 additions and 0 deletions.
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))

0 comments on commit 3be3ae6

Please sign in to comment.