2017년 9월 19일 화요일

Javascript Solution - Big Sorting Solution

hackkerank Big Sorting Solution


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 compare(a,b){
    if(a.length > b.length){
        return 1
    }
    else if(a.length < b.length){
        return -1
    }
    var unsolve = true,idx = 0
    var char_a,char_b
    while(unsolve){
        char_a = parseInt(a.charAt(idx))
        char_b = parseInt(b.charAt(idx))
        
        if(isNaN(char_a)){
            return 0
        }
        
        if(char_a > char_b){
            return 1
        }
        else if(char_a < char_b){
            return -1
        }
        else{
            idx++
        }
    }
}


function main() {
    var n = parseInt(readLine());
    var unsorted = [];
    for(var unsorted_i = 0; unsorted_i < n; unsorted_i++){
       unsorted[unsorted_i] = readLine();
    }
    // your code goes here
    var sorted = unsorted.sort(compare)
    console.log(sorted.join('\n'))
}

댓글 없음:

댓글 쓰기