Advertisement
Guest User

Untitled

a guest
Jan 31st, 2016
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.     var name,
  3.         type,
  4.         taskNumber,
  5.         score,
  6.         lines;
  7.     var result = {};
  8.     for (var i = 0; i < arr.length; i++) {
  9.         var obj = arr[i];
  10.         var split = obj.split('&');
  11.         name = split[0].trim();
  12.         type = split[1].trim();
  13.         taskNumber = split[2].trim();
  14.         score = Number(split[3].trim());
  15.         lines = Number(split[4].trim());
  16.         taskNumber = 'Task ' + taskNumber;
  17.         if (!(taskNumber in result)) {
  18.             result[taskNumber] = {'tasks': [], 'average': 0, 'lines': 0};
  19.         }
  20.         result[taskNumber]['tasks'].push({'name': name, 'type': type});
  21.         result[taskNumber]['average'] += score;
  22.         result[taskNumber]['lines'] += lines;
  23.     }
  24.     for (var obj1 in result) {
  25.         var avg = result[obj1].average / result[obj1].tasks.length;
  26.         result[obj1].average = Math.round(avg * 100) / 100;
  27.         result[obj1].tasks.sort(function (x, y) {
  28.             return x.name.localeCompare(y.name);
  29.         });
  30.     }
  31.  
  32.     var a = [];
  33.     var i = 0;
  34.     for (var obj2 in result) {
  35.         a.push({});
  36.         a[i][obj2] = result[obj2];
  37.         i++;
  38.     }
  39.     a.sort(function (x, y) {
  40.         var xKey = Object.keys(x)[0];
  41.         var yKey = Object.keys(y)[0];
  42.         if (x[xKey].average !== y[yKey].average) {
  43.             return y[yKey].average - x[xKey].average;
  44.         }
  45.         return x[xKey].lines - y[yKey].lines;
  46.     })
  47.     var json = JSON.stringify(a);
  48.     json = json.substring(1, json.length-1).replace(/}},{/g, '},');
  49.     console.log(json);
  50. }
  51.  
  52. var arr = ['Array Matcher & strings & 4 & 100 & 38',
  53.     'Magic Wand & draw & 3 & 100 & 15',
  54.     'Dream Item & loops & 2 & 88 & 80',
  55.     'Knight Path & bits & 5 & 100 & 65',
  56.     'Basket Battle & conditionals & 2 & 100 & 120',
  57.     'Torrent Pirate & calculations & 1 & 100 & 20',
  58.     'Encrypted Matrix & nested loops & 4 & 90 & 52',
  59.     'Game of bits & bits & 5 & 100 & 18',
  60.     'Fit box in box & conditionals & 1 & 100 & 95',
  61.     'Disk & draw & 3 & 90 & 15',
  62.     'Poker Straight & nested loops & 4 & 40 & 57',
  63.     'Friend Bits & bits & 5 & 100 & 81'];
  64. solve(arr);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement