I need to sort a string based on parenthesis. the given input: "x()y()z(a)" needs to be sorted to "(((xyza)))"
Here is the code I have, but I am getting back "((()))" i understand why this is happening, as making a substring at 1 each time cuts off the character that isn't "(" or an ")"
How am I supposed to keep the other characters, in order, without losing them? I can't use a holding string, since this is a recursive function.
Thanks, lets see if there's anybody relatively smart on here.
Robert Miller
Check how many left/right parentheses there are, then strip them and add them to the remainder text.
Gabriel Rogers
I don't think that would work recursively.
Jose Reed
cuz it doesn't need to be recursive
Angel Walker
But in this case, it has to be.... Anything done in a loop can also be done recursively
Bentley Green
At the end instead of return rest try putting something l
like return str.substring(0,str.find('(') + (check + str.substring(str.find('(')+1))
Jeremiah Mitchell
there's probably a better way, but if you want to use a holding string you can just use a string pointer as a parameter.
Lincoln Martin
The problem provides the parameter, so I'm not allowed to change it.
I know of these things, but the thing is my class hasn't gone into this detail yet; which wouldn't allow me to use .find.
Juan Allen
You're making it far more complicated than it has to be.
Landon Lee
so it has to be recursive?
Ethan Reed
It has to be recursive
Samuel Diaz
You could just put a while loop at the end that checks the index of the first non '(' element and then puts check that is neither '(' nor ')' before it.
Josiah Diaz
It's recursive. No loops can be used.
Austin Wood
>lets see if there's anybody relatively smart on here. seriously?
all you have to do is change the last line to return str;
Wyatt Sullivan
>seriously? That doesn't work.. Did you even read?
Isaac Reed
Then implement that loop recursively
Cameron Roberts
You can't just switch the position of the character like that, this isn't an array.
Mason Young
A string is exactly an array of chars. You can just use two substrings once you know the number of the first letter/last '('. Use another recursive function to get the number and it's easy.
James Stewart
But since this is sorting, the substrings will get smaller, and anything that calls for an index greater than 1 after a couple passes will throw an out of bounds exception.
I think the question is easier by saying, What do i need to return when they aren't "(" or ")"
Ian Rogers
& this has to work for any given input, not just the test case.
Jaxson Miller
>But since this is sorting, the substrings will get smaller, and anything that calls for an index greater than 1 after a couple passes will throw an out of bounds exception. It should be fine if you account for an exception with one if statement or two.
Tyler Parker
This is one way to do it; I'm not sure how it can be done without another recursive function.