Created
October 10, 2018 00:55
-
-
Save TobiahRex/345ac7c46b668d14be888642ddc008eb to your computer and use it in GitHub Desktop.
HackerRank-Parse-Input
This file contains hidden or 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
/* | |
HackerRank standardizes its input stream for many of its challenges. | |
This function can be optimized to account for different input types. | |
Simply change variable names etc. | |
*/ | |
function parseInputs(input) { | |
const inputs = input.split('\n'); | |
const testCases = Number(inputs[0]); | |
const nodes = []; | |
const queries = []; | |
let history = 1; | |
for (let i = 0; i < testCases; i++) { | |
let newNodesLength = Number(inputs[history]); | |
let newNodes = []; | |
for (let j = 0; j < newNodesLength; j++) { | |
history += 1; | |
newNodes.push(inputs[history]); | |
} | |
nodes.push(newNodes); | |
history += 1; | |
let newQueriesLength = Number(inputs[history]); | |
let newQueries = []; | |
for (let k = 0; k < newQueriesLength; k++) { | |
history += 1; | |
newQueries.push(inputs[history]); | |
} | |
queries.push(newQueries); | |
history += 1; | |
} | |
return ({ | |
testCases, | |
nodes, | |
queries, | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment