Skip to content

Instantly share code, notes, and snippets.

@Snack-X Snack-X/validator.js
Created Jul 14, 2014

Embed
What would you like to do?
codegolf - make a number expression - validator
var fs = require("fs");
var input = fs.readFileSync("input.txt", "ascii");
var output = fs.readFileSync("output.txt", "ascii");
input = input.split("\n");
output = output.split("\n");
var OK = 0;
for(var i in output) {
console.log("Case " + (+i+1) + " : " + input[i] + " => " + output[i]);
var I = parseInt(input[i], 10);
var O = output[i];
var stack = [];
for(var j = 0 ; j < O.length ; j++) {
var ch = O[j];
var cc = O.charCodeAt(j);
if(cc >= 50 && cc <= 57) stack.push(cc - 48);
else if(ch == "+") {
stack.push(stack.pop() + stack.pop());
}
else if(ch == "-") {
var n1 = stack.pop();
var n2 = stack.pop();
stack.push(n2 - n1);
}
else if(ch == "*") {
stack.push(stack.pop() * stack.pop());
}
else if(ch == "/") {
var n1 = stack.pop();
var n2 = stack.pop();
stack.push(Math.floor(n2 / n1));
}
else if(ch == ">") {
var n = stack.pop();
stack.push(n, n);
}
}
if(stack[0] == I) {
console.log(" - OK");
OK++;
}
else console.log(" - :(");
}
console.log("Test Result : " + OK + "/" + output.length);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.