Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var readline = require('readline');
- var io_interface = readline.createInterface({input: process.stdin});
- let inputLines = [],
- line_number = 0,
- num_lines;
- io_interface.on('line', function (line) {
- if (line_number === 0) {
- num_lines = parseInt(line);
- } else if (line_number <= num_lines) {
- inputLines.push(line);
- }
- line_number++;
- })
- io_interface.on('close', function () {
- const sortedData = quicksortDesc(inputLines);
- for (const rowData of sortedData) {
- process.stdout.write(rowData.split(' ')[0] + "\n");
- }
- })
- function partition(array, pivot) {
- let left = [], center = [], right = [];
- for (let x of array) {
- if (x == pivot) {
- center.push(x);
- } else if (isItemLessPivot(x, pivot)) {
- left.push(x);
- } else {
- right.push(x);
- }
- }
- return [right, center, left];
- }
- function isItemLessPivot(item, pivot) {
- let [itemName, numberItemTasks, numberItemErrors] = item.split(' ');
- let [pivotName, numberPivotTasks, numberPivotErrors] = pivot.split(' ');
- if (Number(numberItemTasks) < Number(numberPivotTasks)) {
- return true;
- }
- if (
- numberItemTasks == numberPivotTasks
- && (Number(numberItemErrors) > Number(numberPivotErrors))
- ) {
- return true;
- }
- if (
- (
- numberItemTasks == numberPivotTasks
- && numberItemErrors == numberPivotErrors
- )
- && itemName > pivotName
- ) {
- return true;
- }
- return false;
- }
- function quicksortDesc(array) {
- if (array.length < 2) {
- return array;
- } else {
- let pivot = array[Math.floor(array.length/2)];
- let [left, center, right] = partition(array, pivot);
- return quicksortDesc(left).concat(center).concat(quicksortDesc(right));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement