Created
October 2, 2012 23:42
-
-
Save joeegan/3824030 to your computer and use it in GitHub Desktop.
javascript balance parentheses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function balanceA(arr) { | |
| var filtered = arr.filter(function(item) { | |
| return item.match(/(\(|\))/g) | |
| }); | |
| function test(a) { | |
| // these conditions are insane, there must be a far easier way of doing this... | |
| if (a.length == 0) { | |
| return true; | |
| } | |
| else if (a.length % 2 !== 0) { | |
| return false; | |
| } | |
| // last two are ')(' and length is greater than 2 | |
| else if ((a[a.length-2] == ")" && a[a.length-1] == "(") && a.length > 2 ) { | |
| return false; | |
| } | |
| // first two are "()" - remove them and recurse | |
| else if ((a[0] == "(" && a[1] == ")")) { | |
| return test(a.slice(2)); | |
| } | |
| // last two are "()" - remove them and recurse | |
| else if (a[a.length-1] == ")" && a[a.length-2] == "(") { | |
| return test(a.slice(0,-2)); | |
| } | |
| // first two are ')(' and length is greater than 2 | |
| else if ((a[0] == ")" && a[1] == "(") && a.length > 2 ) { | |
| return false; | |
| } | |
| // first is "(", last is ")" - remove them and recurse | |
| else if (a[0] == "(" && a.last() == ")") { | |
| return test(a.slice(1, -1)); | |
| } | |
| // it must be ')(', and that's ok, because it survived previous conditions | |
| else { | |
| return true; | |
| } | |
| } | |
| return test(filtered); | |
| } | |
| function balanceB(arr, unbalancedParentheses) { | |
| var first = arr[0]; | |
| unbalancedParentheses = unbalancedParentheses || 0; | |
| if (first == '(') | |
| unbalancedParentheses++; | |
| else if (first == ')') | |
| unbalancedParentheses--; | |
| if (unbalancedParentheses < 0) | |
| return false; | |
| if (arr.length == 1) | |
| return unbalancedParentheses == 0; | |
| return balanceB(arr.slice(1), unbalancedParentheses); | |
| } | |
| console.log("true..."); | |
| console.log(balanceA("(if (zero? x) max (/ 1 x))".split(""))); | |
| console.log(balanceA("I told him (that it’s not (yet) done). (But he wasn’t listening)".split(""))); | |
| console.log(balanceA("(())()".split(""))); | |
| console.log(balanceA("(())(())".split(""))); | |
| console.log(balanceA("(()()())".split(""))); | |
| console.log(balanceA("(((()())))".split(""))); | |
| console.log(balanceA("(()((()())))".split(""))); | |
| console.log(balanceB("(if (zero? x) max (/ 1 x))".split(""))); | |
| console.log(balanceB("I told him (that it’s not (yet) done). (But he wasn’t listening)".split(""))); | |
| console.log(balanceB("(())()".split(""))); | |
| console.log(balanceB("(())(())".split(""))); | |
| console.log(balanceB("(()()())".split(""))); | |
| console.log(balanceB("(((()())))".split(""))); | |
| console.log(balanceB("(()((()())))".split(""))); | |
| console.log("false..."); | |
| console.log(balanceA(":-)".split(""))); | |
| console.log(balanceA("())(".split(""))); | |
| console.log(balanceA("(()())((()()))))".split(""))); | |
| console.log(balanceB(":-)".split(""))); | |
| console.log(balanceB("())(".split(""))); | |
| console.log(balanceB("(()())((()()))))".split(""))); |
Author
function balance(arr, closeparens) {
var tail = arr.slice(1),
next = arr[0];
closeparens = closeparens || 0;
if (next == '(') {
closeparens++;
} else if (next == ')') {
closeparens--;
}
if (closeparens < 0) {
return false;
}
if (arr.length == 1) {
return closeparens == 0;
}
return balance(tail, closeparens);
}
Author
rename 'closeparens' to 'unbalancedParentheses'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
return test(y, lastAtTheFrontWasClosed, lastAtTheEndWasClosed);