Skip to content

Instantly share code, notes, and snippets.

@JeffersGlass
Created January 3, 2021 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffersGlass/8cf3a739a7088e5265587cac1897587c to your computer and use it in GitHub Desktop.
Save JeffersGlass/8cf3a739a7088e5265587cac1897587c to your computer and use it in GitHub Desktop.
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