+ 2

How can we find the index of opening and closing of a parenthesis of a mathematical expression by using phython?

Your expression should contain nested paranthesis

10th Nov 2020, 2:46 AM
Mohammad Shireen
Mohammad Shireen - avatar
6 Antworten
+ 3
Plz show some code related your questions
10th Nov 2020, 2:50 AM
Sâñtôsh
Sâñtôsh - avatar
+ 1
Can any find the program.. Try to say me..
10th Nov 2020, 2:48 AM
Mohammad Shireen
Mohammad Shireen - avatar
+ 1
a=["(27+34 *2+(21*6))" ] b=[] if open parenthesis is equal to the list Then the index should be append to be Other close parenthesis is equal to the a then we should pop to b
10th Nov 2020, 3:22 AM
Mohammad Shireen
Mohammad Shireen - avatar
+ 1
Just start from your open parenthesis take a counter starting from 1, and walk through following characters. Whenever you see an opening parenthesis increase the counter by one, and for a closing parenthesis - decrease the counter by 1. The closing parenthesis on which the counter first becomes 0 is your matching parenthesis. If you need a match for each open parenthesis in an expression, you can do it in one loop using a stack instead of a counter. Each time you see open parenthesis you push its index in the stack, each time you see the closing parenthesis you pop the top open parenthesis index from the stack and match it with the index of the closing parenthesis
10th Nov 2020, 4:46 AM
Volodymyr Chelnokov
Volodymyr Chelnokov - avatar
+ 1
Can you send me the code it's for mathematical expression... so iam unable to convert string to a int
10th Nov 2020, 5:41 AM
Mohammad Shireen
Mohammad Shireen - avatar
+ 1
https://code.sololearn.com/cWFY34xv3mHn/?ref=app This is the code i have written.. But iam unable to strore the values
10th Nov 2020, 6:17 AM
Mohammad Shireen
Mohammad Shireen - avatar