2017년 9월 19일 화요일

Javascript Solution - Insertion Sort - Part 1

hackkerank Insertion Sort - Part 1


function insertSortLog(arr){
    var n = arr.length-1
    var unsorted = true
    var tmp = arr[n]
    var log = []
    while(unsorted){
        if(tmp < arr[n-1]) {
            arr[n] = arr[n-1]
            n--
        } else {
            arr[n] = tmp
            unsorted = false
        }
        console.log(arr.join(' '))
    }
}

function processData(input) {
    //Enter your code here
    input = input.split('\n')
    var n = input[0]
    var arr = input[1].split(' ')
    insertSortLog(arr)
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

Javascript Solution - Intro to Tutorial Challenges

hackkerank Intro to Tutorial Challenges


function processData(input) {
    //Enter your code here
    input = input.split('\n')
    var V = input[0]
    var n = input[1]
    var ar = input[2].split(' ')
    console.log(ar.indexOf(V))
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});