Oct-24-2018, 01:00 AM
So I am working on a program that takes function calls like:
add(2,2)
multiply(1,2)
add(add(2,2),6)
multiply(add(add(1,3),2),3)
add(add(2,2),add(1,1))
My current code can solve the first 2 just fine and I have an idea on how to solve the ones with the nested functions using recursion, but I can't figure out how to split the string up for the nested calls.
My current function just takes in a string, then gets each value from between the parentheses,
so for the first example I have:
val1 = 2
val2 = 2
so what I need to do for a line like the last one is have it split it so that:
val1 = add(2,2)
val2 = add(1,1)
I figure in my current code I could add an if statement that checks to see if each value contains "add" or "multiply", then call the processing function recursively so that it could evaluate it and return the result.
so some pseudo code for that would be something like:
def f(str):
val1 = left side of comma #add(2,2) for first run
val2 = right side of comma #add(1,1) for first run
if(val1.find("add") != -1):
val1 = f(val1)
elif(val2.find("add") != -1):
val2 = f(val2)
elif(val1 == number and val2 == number):
Result = val1 + val2
return Result
f(add(add(2,2),add(1,1))
This is basically what I'm thinking so far on that aspect of my program, but before I can work on figuring that out I must first figure out how to split the line correctly.
Here is what I just tried:
1: add(2,2),add(3
2: 3)
The above code works if st were something like "add(add(1,2),6)", but not if there's another add function in there. I see why, I'm just not sure how to fix it. Then there's how I would deal with something like "multiply(add(add(1,3),2),3)".
Maybe I need to find the last '(' then the first ')' get the result of that, and replace the function call with it's answer, then repeat? Like multiply(add(add(1,3),2),3) => multiply(add(4,2),3) => multiply(6,3) => 18
But besides the parentheses I need to check what operation I'm supposed to perform. Would it work to find last operation? Like I check last of '(' and first of ')' to get 1,3 then I check last operation and get "add" so then I perform add on 1 and 3. But I'm not sure how to get it to check for either function. Like if I were to check last of "multiply", then that wouldn't give me the correct result because I need last of "add". The reason I haven't just tested the above is because I don't know the python code for find_first_of and find_last_of. From what I can tell Python doesn't have functions built in for that like C++, so I'm not sure how to do it.
I'm not asking for code, although it would be appreciated, I'm mainly just looking for ideas because I'm still new to Python.
The main thing I need help on is the string splitting, but if you have any advice on my recursive evaluation idea then I would appreciate that as well.
Sorry if my explanation seems a little sloppy, I am pretty tired at the time of posting this. I'd wait until morning to post this, but I wanted to get it up as soon as possible. I'll look at it in the morning and clean it up as needed.
Thanks in advance.
add(2,2)
multiply(1,2)
add(add(2,2),6)
multiply(add(add(1,3),2),3)
add(add(2,2),add(1,1))
My current code can solve the first 2 just fine and I have an idea on how to solve the ones with the nested functions using recursion, but I can't figure out how to split the string up for the nested calls.
My current function just takes in a string, then gets each value from between the parentheses,
so for the first example I have:
val1 = 2
val2 = 2
so what I need to do for a line like the last one is have it split it so that:
val1 = add(2,2)
val2 = add(1,1)
I figure in my current code I could add an if statement that checks to see if each value contains "add" or "multiply", then call the processing function recursively so that it could evaluate it and return the result.
so some pseudo code for that would be something like:
def f(str):
val1 = left side of comma #add(2,2) for first run
val2 = right side of comma #add(1,1) for first run
if(val1.find("add") != -1):
val1 = f(val1)
elif(val2.find("add") != -1):
val2 = f(val2)
elif(val1 == number and val2 == number):
Result = val1 + val2
return Result
f(add(add(2,2),add(1,1))
This is basically what I'm thinking so far on that aspect of my program, but before I can work on figuring that out I must first figure out how to split the line correctly.
Here is what I just tried:
st = "add(add(2,2),add(3,3))" print("1: ", st[st.find('(')+1:st.rfind(',')]) print("2: ", st[st.rfind(',')+1:st.rfind(')')])the output was:
1: add(2,2),add(3
2: 3)
The above code works if st were something like "add(add(1,2),6)", but not if there's another add function in there. I see why, I'm just not sure how to fix it. Then there's how I would deal with something like "multiply(add(add(1,3),2),3)".
Maybe I need to find the last '(' then the first ')' get the result of that, and replace the function call with it's answer, then repeat? Like multiply(add(add(1,3),2),3) => multiply(add(4,2),3) => multiply(6,3) => 18
But besides the parentheses I need to check what operation I'm supposed to perform. Would it work to find last operation? Like I check last of '(' and first of ')' to get 1,3 then I check last operation and get "add" so then I perform add on 1 and 3. But I'm not sure how to get it to check for either function. Like if I were to check last of "multiply", then that wouldn't give me the correct result because I need last of "add". The reason I haven't just tested the above is because I don't know the python code for find_first_of and find_last_of. From what I can tell Python doesn't have functions built in for that like C++, so I'm not sure how to do it.
I'm not asking for code, although it would be appreciated, I'm mainly just looking for ideas because I'm still new to Python.
The main thing I need help on is the string splitting, but if you have any advice on my recursive evaluation idea then I would appreciate that as well.
Sorry if my explanation seems a little sloppy, I am pretty tired at the time of posting this. I'd wait until morning to post this, but I wanted to get it up as soon as possible. I'll look at it in the morning and clean it up as needed.
Thanks in advance.