I am trying to implement a function for balancing parentheses of a given math equation as a string. It should be changing the string, not just checking if it is balanced.
Because the math equation can contain trigonometric functions, I want to add radians()
after such functions, because in Python, trigonometric functions take input as radians, while I want degrees.
So tan(65)
becomes tan(radians(65))
.
cos(65) + sin(35)
becomes cos(radians(65)) + sin(radians(35))
cos((30 - 10) * 2)
becomes cos(radians((30 - 10) * 2))
So far, what I've done is using replace()
to replace cos(
with cos(radians(
, sin(
with sin(radians(
and the same thing goes for all the rest trigonometric functions. But the problem is, the string (which is a math equation) becomes parentheses-unbalanced.
How do I write a function to solve this problem?