Skip to content

Instantly share code, notes, and snippets.

@joeegan
Created October 2, 2012 23:42
Show Gist options
  • Select an option

  • Save joeegan/3824030 to your computer and use it in GitHub Desktop.

Select an option

Save joeegan/3824030 to your computer and use it in GitHub Desktop.
javascript balance parentheses
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("")));
@joeegan
Copy link
Copy Markdown
Author

joeegan commented Oct 3, 2012

return test(y, lastAtTheFrontWasClosed, lastAtTheEndWasClosed);

@adamjmcgrath
Copy link
Copy Markdown

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);
}

@joeegan
Copy link
Copy Markdown
Author

joeegan commented Oct 5, 2012

rename 'closeparens' to 'unbalancedParentheses'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment