Created
February 16, 2016 12:08
-
-
Save yashikagarg13/c1d0ddc9e8745f111b1e to your computer and use it in GitHub Desktop.
Hacker Rank - JS Challenged
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
function processData(input) { | |
var flag = true; | |
var rows = 8; | |
var str = ''; | |
for(var i=0; i<=rows; i++) { | |
for(j=rows-i; j > 0; j--) { | |
str += ' '; | |
} | |
if (i == 0 && flag) { | |
str += '*'; | |
i = -1; | |
flag = false; | |
str += '\n'; | |
continue; | |
} | |
for(var k=0; k < i*2+1; k++) { | |
str += '0'; | |
} | |
str += '\n'; | |
} | |
console.log(str); | |
} | |
process.stdin.resume(); | |
process.stdin.setEncoding("ascii"); | |
_input = ""; | |
process.stdin.on("data", function (input) { | |
_input += input; | |
}); | |
process.stdin.on("end", function () { | |
processData(_input); | |
}); |
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
function processData(input) { | |
var inputArr = input.split("\n"); | |
var m = parseInt(inputArr[0]); | |
var n = parseInt(inputArr[1]); | |
var matrix = []; | |
for(var i = 0; i < m; i++ ) { | |
matrix.push(inputArr[i+2].split(" ").map(n => parseInt(n))); | |
} | |
var result = [].concat(saveRegion(0, 0, matrix, m, n)); | |
result = result.sort(function(a,b) {return b-a}); | |
console.log(result[0]); | |
} | |
function saveRegion(rIdx, cIdx, matrix, m, n) { | |
var result = []; | |
var cells = isRegion (rIdx, cIdx, matrix, m, n); | |
result.push(cells[0]); | |
var newC = cells[2]+1 < n ? cells[2]+1 : 0; | |
var newR = cells[2]+1 < n ? cells[1] : (cells[1]+1 < m ? cells[1]+1 : m); | |
if(newR < m) { | |
result = result.concat(saveRegion (newR, newC, matrix, m, n)); | |
return result; | |
} else { | |
return result; | |
} | |
} | |
function isRegion (rIdx, cIdx, matrix, m, n) { | |
var count = 0; | |
if (!isValid(rIdx, m) || !isValid(cIdx, n)) { | |
return [count, rIdx, cIdx]; | |
} | |
if (matrix[rIdx][cIdx] == 0 || matrix[rIdx][cIdx] == 'X') { | |
return [count, rIdx, cIdx]; | |
} | |
if (matrix[rIdx][cIdx] == 1) { | |
matrix[rIdx][cIdx] = 'X'; | |
++count; | |
} | |
count += isRegion (rIdx-1, cIdx-1, matrix, m, n)[0]; | |
count += isRegion (rIdx-1, cIdx, matrix, m, n)[0]; | |
count += isRegion (rIdx-1, cIdx+1, matrix, m, n)[0]; | |
count += isRegion (rIdx, cIdx-1, matrix, m, n)[0]; | |
count += isRegion (rIdx, cIdx+1, matrix, m, n)[0]; | |
count += isRegion (rIdx+1, cIdx-1, matrix, m, n)[0]; | |
count += isRegion (rIdx+1, cIdx, matrix, m, n)[0]; | |
count += isRegion (rIdx+1, cIdx+1, matrix, m, n)[0]; | |
return [count, rIdx, cIdx]; | |
} | |
function isValid(idx, max) { | |
return (idx >= 0 && idx < max); | |
} | |
process.stdin.resume(); | |
process.stdin.setEncoding("ascii"); | |
_input = ""; | |
process.stdin.on("data", function (input) { | |
_input += input; | |
}); | |
process.stdin.on("end", function () { | |
processData(_input); | |
}); |
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
function processData(input) { | |
var inpArr = input.split("\n"); | |
var t = inpArr[0]; | |
for(var i=1; i<=t; i++){ | |
var str = inpArr[i]; | |
if(str.split("").reverse().join("") == str){ | |
console.log(0); | |
continue; | |
} | |
var operations = 0; | |
for(var j=0, k=str.length-1; j < str.length/2; j++, k--) { | |
if(str[j] == str[k]) continue; | |
operations += Math.abs(str.charCodeAt(j) - str.charCodeAt(k)); | |
} | |
console.log(operations); | |
} | |
} | |
process.stdin.resume(); | |
process.stdin.setEncoding("ascii"); | |
_input = ""; | |
process.stdin.on("data", function (input) { | |
_input += input; | |
}); | |
process.stdin.on("end", function () { | |
processData(_input); | |
}); |
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
function processData(input) { | |
var N = parseInt(input.split("\n")[0].split(" ")[0]); | |
var K = parseInt(input.split("\n")[0].split(" ")[1]); | |
var arr = input.split("\n")[1].split(" "); | |
arr = arr.map(function(i) { | |
return parseInt(i); | |
}); | |
arr = arr.sort(function(a,b) { | |
return a-b; | |
}); | |
var numberOfToys = 0; | |
arr.reduce(function(memo, i) { | |
if(memo + i <= K) { | |
++numberOfToys; | |
return (memo+i) | |
} else { | |
return memo; | |
} | |
}, 0, arr) | |
console.log(numberOfToys); | |
} | |
process.stdin.resume(); | |
process.stdin.setEncoding("ascii"); | |
_input = ""; | |
process.stdin.on("data", function (input) { | |
_input += input; | |
}); | |
process.stdin.on("end", function () { | |
processData(_input); | |
}); |
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
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++]; | |
} | |
/////////////// ignore above this line //////////////////// | |
function main() { | |
var t = parseInt(readLine()); | |
for(var a0 = 0; a0 < t; a0++){ | |
var n = parseInt(readLine()); | |
var height = 1; | |
for(var i=0; i<n; i++) { | |
if(i%2 == 0) { | |
height *= 2; | |
} else { | |
++height; | |
} | |
} | |
console.log(height); | |
} | |
} |
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
function processData(input) { | |
var str = ''; | |
for(var i=0; i<14; i++) { | |
for(var j=0; j<7; j++) { | |
str += '\u2571\u2572'; | |
} | |
str += '\n'; | |
} | |
console.log(str); | |
} | |
process.stdin.resume(); | |
process.stdin.setEncoding("ascii"); | |
_input = ""; | |
process.stdin.on("data", function (input) { | |
_input += input; | |
}); | |
process.stdin.on("end", function () { | |
processData(_input); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment