Commit 788c1cb8 authored by Ilia Prokhorov's avatar Ilia Prokhorov

Binary search implemented

parent 14704970
console.log("Hello World");
\ No newline at end of file
process.stdin.on('data', function(data) {
var rawInput = data.toString('utf8');
var inputs = rawInput.split("\n");
filterInputs(inputs);
});
function filterInputs(inputs) {
var whitelist = ["[email protected]", "[email protected]", "[email protected]", "[email protected]"];
var sortedWhitelist = [...whitelist].sort()
inputs.forEach(input => {
search(input, sortedWhitelist, 0, sortedWhitelist.length)
})
}
function search(word, sortedWhitelist, lowIndex, highIndex) {
if (highIndex <= lowIndex) {
console.log("-No Match-")
}
var middleIndex = lowIndex + (highIndex - lowIndex) / 2;
var middleWhitelistedWord = sortedWhitelist[middleIndex];
if (middleWhitelistedWord.hasOwnProperty("localeCompare") == false) {
return;
}
var compareResult = middleWhitelistedWord.localeCompare(word);
if (compareResult > 0) {
search(word, sortedWhitelist, lowIndex, middleIndex);
}
else if (compareResult < 0) {
search(word, sortedWhitelist, middleIndex + 1, highIndex);
}
else {
console.log("Found");
console.log(word);
}
}
cd C:\Users\demen\Documents\nodejs
node.exe C:\Users\demen\Documents\algorithms\whitelist\BinarySearch\binarySearch.js
@pause
\ No newline at end of file
scalac ../Generator/Generator.scala
trap times EXIT;
scala -J-Xmx2g GeneratorApp 10 10 | node binarySearch.js
......@@ -8,6 +8,9 @@ class Generator {
val string = Random.alphanumeric.take(wordLength).mkString("")
wordsList += string
}
wordsList += "[email protected]"
return wordsList.toArray
}
}
......
if [ ! -f GeneratorApp.class ]; then
scalac ../Generator/Generator.scala
fi
scalac ../Generator/Generator.scala
trap times EXIT;
scala GeneratorApp 500000 80 | node naiveSearch.js
scala -J-Xmx2g GeneratorApp 10 10 | node naiveSearch.js
......@@ -14,7 +14,9 @@ function search(word) {
var whitelist = ["[email protected]", "[email protected]", "[email protected]", "[email protected]"];
whitelist.forEach(whitelistWord => {
if (whitelistWord === word) {
console.log("Found")
console.log(word)
return;
}
})
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment