-
-
Save JeffersGlass/8cf3a739a7088e5265587cac1897587c 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 | |
#We will be looking to evaluate strings like: | |
eq1 = "1 + (2 * 3) + (4 * (5 + 6))" | |
eq2 = "5 + (8 * 3 + 9 + 3 * 4 * 3)" | |
#Parentheses take preference, then strictly left-to-right | |
#Let's find the innermost set of parens to evaluate | |
def findInnerParens(equation): | |
pattern = re.compile(r'') | |
return re.search(pattern, equation) | |
print(findInnerParens(eq1)) | |
print(findInnerParens(eq2)) | |
#Given an equation with no parentheses, find the first two numbers | |
#So we can do an operation on them | |
def findFirstTwo(equation): | |
pattern = re.compile(r'') | |
return re.search(pattern, equation) | |
eq3 = '4 + 63 * 7 + 8' | |
eq4 = '51 * 7 - 14' | |
print(findFirstTwo(eq3)) | |
print(findFirstTwo(eq4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment