Last active
September 3, 2016 20:24
-
-
Save paveleremin/ea8685a9141f056c72b7 to your computer and use it in GitHub Desktop.
Siteimprove test
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
'strict mode'; | |
/** | |
* > You choose programming language, any external libraries | |
* > and strategi for implementation. | |
* > You should build a solution from scratch. | |
* | |
* I chose Javascript that work on a server (NodeJS) | |
*/ | |
const fs = require('fs'); | |
const path = require('path'); | |
const readline = require('readline'); | |
const stream = require('stream'); | |
const pattern = /Section \d+ Refresh/; | |
const inputPath = './input'; | |
const files = fs.readdirSync(inputPath); | |
const ids = []; | |
const promises = []; | |
/** | |
* > Assume that there's no upper limit on the length of an inputfile. | |
* | |
* In this case, I'm reading a file line by line, to prevent memory limit server error | |
*/ | |
files.forEach((file) => { | |
var promise = new Promise((resolve) => { | |
const filePath = path.resolve(__dirname, inputPath, file); | |
const rl = readline.createInterface(fs.createReadStream(filePath), new stream); | |
rl.on('line', (line) => { | |
/** | |
* > Scan or search through a set of textfiles in the format described above | |
* > after a given search pattern (in regular expression syntax). | |
* > If a linie in an input file matches the pattern, return the id of the | |
* > matching line. Neither the name of the file, the content of the line | |
* > or the order of the resulting id's matter. | |
* | |
* I had strict rules about the file format from your side. | |
* So I'm checking the line "as it is" | |
* to avoid split() or substr() operations and save the CPU | |
*/ | |
if (!line.match(pattern)) return; | |
const id = line.substr(0, line.indexOf(':')); | |
ids.push(id); | |
}); | |
rl.on('close', () => { | |
resolve(); | |
}); | |
}); | |
promises.push(promise); | |
}); | |
/** | |
* > Try to achieve optimal ressource (min IO, min RAM, max CPU) utilization, | |
* > by eg. threading or other strategies of your choice. | |
* | |
* I used an async aproach to improve speed of process | |
*/ | |
Promise.all(promises).then(() => { | |
console.log('Input:', files.join(' ')); | |
console.log('Search:', pattern); | |
console.log('Results:', ids.join()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment