• Post Reply Bookmark Topic Watch Topic
  • New Topic

Find matching parenthesis  RSS feed

 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Below is my code to print the location of the matching paranthesis. I checked it for a few inputs and it seems to work. I was wondering if there's a way to make my code better.

 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It just struck me that the Stack is a bit redundant there, so here is my modified code:

 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Raman wrote:Below is my code to print the location of the matching paranthesis.

Hi. I don't seem fully understand the problem.

1. So what is the answer according to your logic to this string: "(()(()))"?
2. What would happen with string ")(()"?
3. How many positions suppose to be printed out in success case?
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
1. So what is the answer according to your logic to this string: "(()(()))"?
The input must be a String and an index (index of the paranthesis whose matching parenthesis' index is returned by the function)

Liutauras Vilda wrote:3. How many positions suppose to be printed out in success case?
- Just 1.
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The logic seems sound (I didn't check the edge cases) but your formatting is bad. This is almost as bad as poor logic. Be should to indent properly and line up code under the same control structure.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, it seems works on a perfect cases
Next step probably would be to check first, if it is a valid mathematical expression.
Because:
")(()" - doesn't give me an expected result.

-edit- Knute just mentioned before me, it doesn't seem you thought about the corner cases
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, Knute! I was using that editor for the first time, hence a bit tardy. Here is a better formatted version:

 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this supposed to be for parsing? What are you doing when you have unmatched brackets? How are you handling )(() as suggested earlier?
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, this is just a challenge problem that I came across somewhere. Unmatched brackets should return -1. )(() doesn't matter because you give the starting parenthesis' position as input to the function, so if it finds the matching one, it returns its position else returns -1.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaaaaaaaaaaah. you should have told us that earlier. I had to do it for parsing, and had to deal with things like “)))))” where the brackets inside the String literal don't count.

I think you are probably all right with what you have already.
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!! And sorry about not mentioning practice problem before!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!