[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Hello /sci/ I need to sort a string based on parenthesis. the

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 24
Thread images: 2

Hello /sci/

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.
>>
Check how many left/right parentheses there are, then strip them and add them to the remainder text.
>>
>>8742548
I don't think that would work recursively.
>>
cuz it doesn't need to be recursive
>>
>>8742573
But in this case, it has to be.... Anything done in a loop can also be done recursively
>>
>>8742535
At the end instead of return rest try putting something l

like return str.substring(0,str.find('(') + (check + str.substring(str.find('(')+1))
>>
>>8742535
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.
>>
>>8742611
>>8742635

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.
>>
>>8742535
You're making it far more complicated than it has to be.
>>
so it has to be recursive?
>>
>>8742683
>>8742684

It has to be recursive
>>
>>8742652
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.
>>
>>8742690
It's recursive. No loops can be used.
>>
>>8742535
>lets see if there's anybody relatively smart on here.
seriously?

all you have to do is change the last line to
return str;
>>
>>8742695

>seriously?
That doesn't work.. Did you even read?
>>
>>8742694
Then implement that loop recursively
>>
>>8742701
You can't just switch the position of the character like that, this isn't an array.
>>
>>8742702
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.
>>
>>8742708
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 ")"
>>
>>8742708
& this has to work for any given input, not just the test case.
>>
>>8742716
>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.
>>
File: ss+(2017-03-13+at+02.47.53).png (23KB, 607x496px) Image search: [Google]
ss+(2017-03-13+at+02.47.53).png
23KB, 607x496px
This is one way to do it; I'm not sure how it can be done without another recursive function.
>>
>>8742535
>java

go fucking kill yourself
>>
why would you do it recursively, that's stupid
Thread posts: 24
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.