I have recently started learning python and am trying to build a simple function to break up a math problem into smaller parts. For example if the problem is (1+2)+(2+4) it would break it up and store it in an array: ["(1+2)", "+", "(2+4)"]. Below ill post the code i have so far.
code:
def crush(string):
crushed_string = []
for i in range(0, len(string)):
current_problem = ""
if string[i] == "(":
for x in range(i, len(string)):
if string[x] == ")":
for y in range(i, x+1):
current_problem += string[y]
print(current_problem)
string = "(1+2)+(3+4)"
crush(string)
I am printing current_problem to the terminal instead of adding it to an array just for testing but the output i am getting doesn't make sense to me...
the output:
(1+2)
(1+2)(1+2)+(3+4)
(3+4)
I'm not looking for a code dump but rather to understand what is happening and why so i can improve and not just copy paste. If anyone could please explain why this is the output i am getting i would be really grateful.
note: I know i haven't added the + between the brackets yet but I've ran into this problem before I've gotten to that and would like to first understand what is happening before moving on.