Advertisement
dimipan80

Exams - All That Luggage

Jan 12th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* The distance between Anchova Bichkiya Hut and Momina Polyana Hut is 3 hours in walking.
  2.  All the people that wanted to spend the New Year's Eve there must transfer their luggage to Momina Polyana Hut –
  3.  all the food, all the drinks, everything. The real tourists transferred their stuff in their backpacks.
  4.  The lazy ones did that by using an ATV machine.
  5.  Your task is to give information about the luggage to everybody that asks you, because you are the leader
  6.  of the group.
  7.  Fortunately everybody writes down everything about their own luggage on sheets of paper.
  8.  You are given a list of luggage names on separate sheets given as text table with the following columns:
  9.  [owner name].*.[luggage name] .*. [is food] .*.[is drink] .*.[is fragile] .*.[weight in kg] .*.[transferred with].
  10.  You are given sorting criterion as a string on the last line of the text table. The different columns
  11.  will always be separated by exactly one '*' sign and 0 or more '.' sign on the left and 0 or more '.' sign on
  12.  the right side of the '*' sign (for example valid separators are: '*', '.*', '*…….', '...*…..').
  13.  The [owner name] and [luggage name] are strings. The [is food], [is drink], [is fragile] are strings -
  14.  either 'true' or 'false'. If 'is food' is 'true', the type of the luggage is 'food'. If 'is drink' is 'true',
  15.  the type of the luggage is 'drink' ('is drink' and 'is food' cannot both be 'true'). If 'is food' and 'is drink'
  16.  are false, the type of the luggage is 'other'. The [weight] is floating-point number.
  17.  The [transferred with] is a string – either 'backpack' or 'ATV'.
  18.  The sorting criterion can be 'luggage name', 'weight' or 'strict'.
  19.  Aggregate the results and print them as JSON string. The order of the owner names is the order of appearance
  20.  in the input. The order of the luggage pieces is according to the sorting criterion.
  21.  If the sorting criterion is 'luggage name', the luggage pieces are ordered alphabetically by their name.
  22.  If the sorting criterion is 'weight', the luggage pieces are ordered by their weight in ascending order.
  23.  If the sorting criterion is 'strict', the luggage pieces are ordered by their order of appearance in the input. */
  24.  
  25. "use strict";
  26.  
  27. function solve(args) {
  28.     var luggage = {};
  29.     var sortCriterion = args[args.length - 1];
  30.    
  31.     for (var i = 0; i < args.length - 1; i += 1) {
  32.         var inputLine = args[i].split(/\.*\*\.*/g);
  33.         var ownerName = inputLine[0];
  34.         var luggageName = inputLine[1];
  35.         var luggageType = (inputLine[2] == 'true') ? 'food' : (inputLine[3] == 'true') ? 'drink' : 'other';
  36.         var fragille = (inputLine[4] == 'true');
  37.        
  38.         if (!(ownerName in luggage)) {
  39.             luggage[ownerName] = {};
  40.         }
  41.        
  42.         luggage[ownerName][luggageName] = {
  43.             'kg': parseFloat(inputLine[5]),
  44.             'fragile': fragille,
  45.             'type': luggageType,
  46.             'transferredWith': inputLine[6]
  47.         };
  48.     }
  49.    
  50.     if (sortCriterion != 'strict') {
  51.         luggage = sortingObject(luggage, sortCriterion);
  52.     }
  53.    
  54.     console.log(JSON.stringify(luggage));
  55.    
  56.     function sortingObject(object, criterion) {
  57.         var newObject = {};
  58.         Object.keys(object).forEach(function (obj) {
  59.             newObject[obj] = {};
  60.             var sorted = Object.keys(object[obj]);
  61.             if (criterion == 'luggage name') {
  62.                 sorted.sort();
  63.             } else if (criterion == 'weight') {
  64.                 sorted.sort(function (a, b) {
  65.                     return object[obj][a].kg - object[obj][b].kg;
  66.                 });
  67.             }
  68.            
  69.             sorted.forEach(function (key) {
  70.                 newObject[obj][key] = object[obj][key];
  71.             });
  72.         });
  73.        
  74.         return newObject;
  75.     }
  76. }
  77.  
  78. solve([
  79.     'Yana Slavcheva.*.clothes.*.false.*.false.*.false.*.2.2.*.backpack',
  80.     'Kiko.*.socks.*.false.*.false.*.false.*.0.2.*.backpack',
  81.     'Kiko.*.banana.*.true.*.false.*.false.*.3.2.*.backpack',
  82.     'Kiko.*.sticks.*.false.*.false.*.false.*.1.6.*.ATV',
  83.     'Kiko.*.glasses.*.false.*.false.*.true.*.3.*.ATV',
  84.     'Manov.*.socks.*.false.*.false.*.false.*.0.3.*.ATV',
  85.     'strict'
  86. ]);
  87.  
  88. solve([
  89.     'Yana Slavcheva.*.clothes.*.false.*.false.*.false.*.2.2.*.backpack',
  90.     'Kiko.*.socks.*.false.*.false.*.false.*.0.2.*.backpack',
  91.     'Kiko.*.banana.*.true.*.false.*.false.*.3.2.*.backpack',
  92.     'Kiko.*.sticks.*.false.*.false.*.false.*.1.6.*.ATV',
  93.     'Kiko.*.glasses.*.false.*.false.*.true.*.3.*.ATV',
  94.     'Manov.*.socks.*.false.*.false.*.false.*.0.3.*.ATV',
  95.     'weight'
  96. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement