-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4830 from espinoleandroo/main
Reto #28 - python
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
Retos/Reto #28 - EXPRESIÓN MATEMÁTICA [Media]/python/EspinoLeandroo.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,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() |