Skip to content

Instantly share code, notes, and snippets.

@lnfnunes
Last active June 7, 2017 05:00
Show Gist options
  • Save lnfnunes/2bc266be8280c853e07f62d7e41fbbae to your computer and use it in GitHub Desktop.
Save lnfnunes/2bc266be8280c853e07f62d7e41fbbae to your computer and use it in GitHub Desktop.
HackerRank JS default snippet
function flatten(arr) { return [].concat.apply([], arr); }
// Usage: flatten([[1,2,3], [1,2], 3, 4])
// Output: [1, 2, 3, 1, 2, 3, 4]
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) { input_stdin += data; });
process.stdin.on('end', function () { input_stdin_array = input_stdin.split("\n"); main(); });
function readLine() { return input_stdin_array[input_currentline++]; }
function getLen() { return Number(readLine()); }
function getArray(separator = ' ') { return readLine().split(separator).map(Number); }
/////////////// ignore above this line ////////////////////
function main() {
let len = getLen();
let arr = getArray();
for (let i=0; i<len; i++) {
console.log(i, arr[i]);
}
}
// Fix JS numeric native sorting
// nok: [-300, -20, 10, 321, -500, 5].sort() // [-20, -300, -500, 10, 321, 5]
// ok: [-300, -20, 10, 321, -500, 5].sort(sortFix) // [-500, -300, -20, 5, 10, 321]
function sortFix(x, y) { return x - y; }
function combinations(str) {
var fn = function(active, rest, a) {
if (!active && !rest) return;
if (!rest) {
a.push(active);
} else {
fn(active + rest[0], rest.slice(1), a);
fn(active, rest.slice(1), a);
}
return a;
}
return fn("", str, []);
}
// Usage: combinations([1, 2, 2].join(''))
// Output: [ '122', '12', '12', '1', '22', '2', '2' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment