I have to write a function that takes a string, and will return the string with added "asteriks" or "*" symbols to signal multiplication.
As we know 4(3) is another way to show multiplication, as well as 4*3 or (4)(3) or 4*(3) etc. Anyway, my code needs to fix that problem by adding an asterik between the 4 and the 3 for when multiplication is shown WITH PARENTHESIS but without the multiplication operator " * ".
Some examples:
- "4(3)" -> "4*(3)"
- "(4)(3)" -> "(4)*(3)"
- "4*2 + 9 -4(-3)" - > "4*2 + 9 -4*(-3)"
- "(-9)(-2) (4)" -> "(-9)*(2) *(4)"
- "4^(3)" -> "4^(3)"
- "(4-3)(4+2)" -> "(4-3)*(4+2)"
- "(Aflkdsjalkb)(g)" -> "(Aflkdsjalkb)*(g)"
- "g(d)(f)" -> "g*(d)*(f)"
- "(4) (3)" -> "(4)*(3)"
I'm not exactly sure how to do this, I am thinking about finding the left parenthesis and then simply adding a " * " at that location but that wouldn't work hence the start of my third example would output "* (-9)" which is what I don't want or my fourth example that would output "4^*(3)". Any ideas on how to solve this problem? Thank you.
Here's something I've tried, and obviously it doesn't work:
while index < len(stringtobeconverted)
parenthesis = stringtobeconverted[index]
if parenthesis == "(":
stringtobeconverted[index-1] = "*"