Created
July 14, 2014 03:25
-
-
Save Snack-X/61cb01e800b0b4ff475e to your computer and use it in GitHub Desktop.
codegolf - make a number expression - validator
This file contains 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
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