Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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