Computer Science Canada Java- Recursion |
Author: | blueberykiwi [ Sat Mar 26, 2005 6:28 pm ] | ||
Post subject: | Java- Recursion | ||
i need help on my java assignment, it's due on Monday...i'm stuck... and i'm kinda failing...sooo need a lotta help on this...thanks
that's the first 2 parts...i kno where to *start* the 3rd part...i have no idea how to start it... Suppose that we are working in an environment in which expressions can be bracketed by any of three types of bracketing operators: ( ) parentheses [ ] brackets { } braces In a properly formed expression, these characters will be properly nested and matched. To determine whether this condition holds, you can ignore all other characters and simply look at the pattern formed by the parentheses, brackets, and braces. In a valid configuration, all the operators match up correctly, as shown in the following example: {([])([()])} The following patterns, however, are illegal for the reasons indicated: (([]) A closing right parenthesis is missing. )( The closing parenthesis precedes the opening parenthesis. {(}) The braces and parentheses are improperly nested. Write a recursive method with the header public static boolean isBalanced (String s) that returns true if the bracketing operators in s are balanced and returns false otherwise. Your method may assume that s contains only the bracketing characters shown above. thanks.... i reallie need the help... |
Author: | blueberykiwi [ Sat Mar 26, 2005 6:37 pm ] |
Post subject: | nvm |
actually can i just get help on the 3rd question? i just got the first 2 questions thanks |
Author: | rizzix [ Sat Mar 26, 2005 6:41 pm ] |
Post subject: | |
use a Stack and push the opening braces into the stack, when u come across a closing brace pop the last entry in the stack and check if its the matching opening brace, if not its an error. edit: wait i didn't realise you had to use recursive functions. |
Author: | wtd [ Sat Mar 26, 2005 7:31 pm ] | ||
Post subject: | |||
Here's an example. Not in Java, since I can't do your homework for you, but hopefully it'll give you a push in the right direction.
|
Author: | rizzix [ Sat Mar 26, 2005 11:22 pm ] |
Post subject: | |
hmm.. i'm not sure but ehm i dont think that covers cases like: "({[]}[])" |
Author: | wtd [ Sat Mar 26, 2005 11:34 pm ] | ||
Post subject: | |||
![]() |
Author: | rizzix [ Sat Mar 26, 2005 11:35 pm ] |
Post subject: | |
nice. ![]() |
Author: | wtd [ Sun Mar 27, 2005 1:26 am ] |
Post subject: | |
Gracias. ![]() |
Author: | 1of42 [ Fri Apr 01, 2005 1:16 pm ] |
Post subject: | |
Well, this isn't recursive, but... Keep 3 variables: 1 keeps track of parentheses, one keeps track of braces, the other keeps track of brackets. Initialize each to 0. Move through the String, adding 1 to a variables any time you encounter one of the characters it represents. Remove 1 on any closing parentheses etyc. Ensure that no variables are ever negative, if any do become negative return false. At the end, if every variables is at value 0, the string is balanced. |
Author: | wtd [ Fri Apr 01, 2005 1:25 pm ] | ||||
Post subject: | |||||
1of42 wrote: Well, this isn't recursive, but...
Keep 3 variables: 1 keeps track of parentheses, one keeps track of braces, the other keeps track of brackets. Initialize each to 0. Move through the String, adding 1 to a variables any time you encounter one of the characters it represents. Remove 1 on any closing parentheses etyc. Ensure that no variables are ever negative, if any do become negative return false. At the end, if every variables is at value 0, the string is balanced. This doesn't work.
Would pass your test, but is not balanced. For what it's worth, though, an implementation of the flawed algorithm. ![]()
|
Author: | 1of42 [ Fri Apr 01, 2005 4:50 pm ] |
Post subject: | |
Hehe... Oh well, I guess that's what I get for not thinking about a problem enough ![]() |
Author: | rizzix [ Tue Apr 12, 2005 7:02 pm ] | ||||
Post subject: | |||||
Think it's safe to post the solution.. i hope so..
here's how you use it:
edit: oops.. long string sorry.. i used that to test the speed of the algorithm |
Author: | rizzix [ Wed Apr 13, 2005 3:30 pm ] |
Post subject: | |
shoot. sorry i thought i had fixed the thread. edit: there we go.. definitely fixed |