Oct-27-2018, 06:50 PM
I believe I have found the answer to my problem: regular expressions.
Remember I said if there was a way to look for all expressions in form add/multiply(int,int) and replace with value? I found out that regular expressions can do that first part.
Here is a simple example of the code I'm using:
I can then replace "add(1,2)" with its value by using string.replace
So I can just put this in a loop and it will eventually get me the answer!
Thank you viewers for the moral support and thank you buran for your earlier reply.
Here is the link to the regular expressions tutorial I watched in case someone else needs it: https://www.youtube.com/watch?v=K8L6KVGG-7o
Remember I said if there was a way to look for all expressions in form add/multiply(int,int) and replace with value? I found out that regular expressions can do that first part.
Here is a simple example of the code I'm using:
import re pattern = re.compile(r'(add|multiply)\([0-9]+,[0-9]+\)') s = multiply(add(1,2), add(3,4)) match = pattern.search(s) print(match.group(0))This will print out "add(1,2)" which I can then pass to my evaluation function to solve.
I can then replace "add(1,2)" with its value by using string.replace
#My evaluation function takes in a string of the form add(int,int) and returns the result in string form s = s.replace(match.group(0), add(match.group(0))after this s will be "multiply(3, add(3,4))
So I can just put this in a loop and it will eventually get me the answer!
Thank you viewers for the moral support and thank you buran for your earlier reply.
Here is the link to the regular expressions tutorial I watched in case someone else needs it: https://www.youtube.com/watch?v=K8L6KVGG-7o