Skip to content

Instantly share code, notes, and snippets.

@animatedlew
Created June 22, 2015 16:54
Show Gist options
  • Save animatedlew/840a78cc1182a2baed3c to your computer and use it in GitHub Desktop.
Save animatedlew/840a78cc1182a2baed3c to your computer and use it in GitHub Desktop.
var data = [
"(if (zero? x) max (/ 1 x))",
"I told him (that it's not (yet) done). (But he wasn't listening)",
":-)",
"())(",
"((d)o(n)e)",
")("
];
function isBalanced(chars) {
var findMatch = (list, isBalanced, total) => {
var b = !(!isBalanced || total < 0);
if (!list.length) return b;
else if (list[0] == '(') return findMatch(list.slice(1), b, total + 1);
else if (list[0] == ')') return findMatch(list.slice(1), b, total - 1);
else return findMatch(list.slice(1), b, total);
}
return findMatch(chars, true, 0);
}
/** TEST SUITE **/
var Mocha = require('mocha');
var expect = require('chai').expect;
var mocha = new Mocha({ui: 'bdd'});
mocha.suite.emit('pre-require', this, 'solution', mocha);
describe("Balanced parentheses", () => {
var r = [true, true, false, false, true, false];
data.forEach((str, i) => {
it("should be " + (r[i] ? "balanced" : "unbalanced"), () => {
var chars = str.split('');
expect(isBalanced(chars)).to.equal(r[i]);
});
});
});
mocha.run(function() {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment