Skip to content

Instantly share code, notes, and snippets.

@nacholibre
Last active August 29, 2015 14:22
Show Gist options
  • Save nacholibre/cbc67a58613e5f525631 to your computer and use it in GitHub Desktop.
Save nacholibre/cbc67a58613e5f525631 to your computer and use it in GitHub Desktop.
brackets
'use strict';
var assert = require('assert');
function validate(chr, prevBracket) {
var valid = true;
if (chr === '(' && prevBracket === '{') {
valid = false;
} else if (chr === '[' && prevBracket === '[') {
valid = false;
} else if (chr === '{' && (prevBracket === '[' || prevBracket === '{')) {
valid = false;
}
return valid;
}
function getStringToEval(expr) {
var evString = '';
var brackets = [];
var prevChar = '';
for (var i = 0; i < expr.length; i++) {
var chr = expr[i];
if (i === 0) {
brackets.push(chr);
continue;
}
var nextChar = expr[i+1];
var prevChr = expr[i-1];
var prevBracket = brackets[brackets.length - 1];
var parsedCharInt = parseInt(chr);
var currentCharValidInt = !isNaN(parsedCharInt);
var nextCharValidInt = !isNaN(parseInt(nextChar));
var endReached = i !== expr.length-1;
if (currentCharValidInt) {
evString += parsedCharInt;
} else if (validate(chr, prevBracket) === false) {
throw Error();
} else {
if (chr === '(') {
if (prevBracket === '[') {
evString += '2*(';
} else {
evString += '(';
}
brackets.push(chr);
} else if (chr === '[') {
if (prevBracket === '{') {
evString += '2*(';
} else {
evString += '(';
}
brackets.push(chr);
} else if ((chr === ')' || chr === ']') && endReached) {
brackets.pop();
if (prevChr === '(') {
evString += '0';
}
evString += ')';
}
}
if (currentCharValidInt && (nextChar === '[' || nextChar === '(')) {
evString += '+';
} else if (chr === ']' && nextChar === '[') {
evString += '+';
} else if ((chr === ')' || chr === ']') && nextCharValidInt) {
evString += '+';
}
prevChar = chr;
}
return evString;
}
function evaluateExpression(expr) {
expr = expr.trim();
var valid = true;
var result;
try {
var evalString = eval(getStringToEval(expr));
result = evalString;
if (!expr) {
throw Error();
}
} catch(e) {
valid = false;
}
if (valid) {
return result;
} else {
return 'NO';
}
}
function runTests() {
console.log('tests---->');
var ev;
ev = evaluateExpression('[125()125()125()125]');
assert(ev === 500);
ev = evaluateExpression('[123(145)38(37)812]');
assert(ev === 1337);
ev = evaluateExpression('(123)');
assert(ev === 123);
ev = evaluateExpression('{125[2][(1)][3]125}');
assert(ev === 264);
ev = evaluateExpression('{125()125}');
assert(ev === 'NO');
ev = evaluateExpression('{125(3[2])125}');
assert(ev === 'NO');
ev = evaluateExpression('[125{}125()125()125]');
assert(ev === 'NO');
ev = evaluateExpression('{125{}125()125()125}');
assert(ev === 'NO');
ev = evaluateExpression('{125[12]{125}[12]125}');
assert(ev === 'NO');
ev = evaluateExpression('{10[2(2)4]20}');
assert(ev === 50);
console.log('tests passed!!!');
}
//runTests();
var stdinData = '';
process.stdin.on('readable', function() {
var chunk = process.stdin.read();
if (chunk !== null) {
stdinData = stdinData + chunk;
} else {
console.log(evaluateExpression(stdinData));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment