Advertisement
Guest User

AllThatLuggage

a guest
Apr 1st, 2015
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     function solve(args) {
  2.  
  3.         /*
  4.             In order sorting criteria to be one word,
  5.             which lately we will use as a property of an object,
  6.             e.g. the object will have properties "weight" and "luggagename",
  7.             so we can directly sort the object by the user input e.g.
  8.             object1[luggagename] > object2[luggagename]
  9.         */
  10.         var criteria = args.pop().replace(" ", "");
  11.  
  12.         /*
  13.             We need an array with objects, for normal sorting.
  14.             It's schema is something like
  15.             [
  16.                 {
  17.                     name: "Kiko",
  18.                     possessions: [
  19.                         {
  20.                             luggagename: "banana", // key used for sorting
  21.                             weight: 3.2, // key used for sorting
  22.                             isFragile: false,
  23.                             type: "food",
  24.                             transferredWith: "backpack"
  25.                         },
  26.                         {
  27.                             luggagename: "glasses", // key used for sorting
  28.                             weight: 3, // key used for sorting
  29.                             isFragile: true,
  30.                             type: "other",
  31.                             transferredWith: "ATV"
  32.                         }
  33.                     ]
  34.                 },
  35.                 {
  36.                     name: "Yana",
  37.                     possessions: [
  38.                         {
  39.                             luggagename: ...,
  40.                             ...,
  41.                             ...
  42.                         }
  43.                     ]
  44.                 }
  45.             ]
  46.         */
  47.         var collection = [];
  48.  
  49.         for (var row in args) {
  50.             var data = args[row].split(/\.*\*\.*/g);
  51.             var name = data[0];
  52.             var posessionName = data[1];
  53.             var isFood = data[2];
  54.             var isDrink = data[3];
  55.             var isFragile = data[4];
  56.             var weight = Number(data[5]);
  57.             var transferedWith = data[6];
  58.  
  59.             /*
  60.                 Determine the type if it's food, drink or other
  61.             */
  62.             var type = "other";
  63.             if (isFood == "true") type = "food";
  64.             else if (isDrink == "true") type = "drink";
  65.  
  66.             /*
  67.                 Each tourist has luggages/possessions,
  68.                 on single iteration we can get only one possession,
  69.                 so we will make an object with its info which lately
  70.                 we will push into the collection of tourist possessions
  71.             */
  72.             var possesion = {
  73.                 luggagename: posessionName, // this key name will be used for sorting later
  74.                 weight: weight, // this key name will be used for sorting later too
  75.                 isFragile: isFragile == "true",
  76.                 type: type,
  77.                 transferredWith: transferedWith
  78.             };
  79.  
  80.             var found = false;
  81.             for (var k in collection) {
  82.  
  83.                 /**
  84.                  * Determine if we have that tourist in the collection
  85.                  * of tourists
  86.                  */
  87.                 if (collection[k]["name"] == name) {
  88.                     for (var p in collection[k]["possessions"]) {
  89.  
  90.                         /**
  91.                          * Determine if a tourist already has a luggage with that name
  92.                          */
  93.                         if (collection[k]["possessions"][p]["name"] == posessionName) {
  94.                             delete collection[k]["possessions"][p]
  95.                             break;
  96.                         }
  97.                     }
  98.  
  99.                     /**
  100.                      * Push the current iteration's luggage into the collection
  101.                      * of possessions (possession == luggage in our terminology)
  102.                      */
  103.                     collection[k]["possessions"].push(possesion);
  104.                     found = true;
  105.                 }
  106.             }
  107.  
  108.             if (!found) {
  109.                 /*
  110.                     If we don't have a tourist with that name, create one
  111.                 */
  112.                 var tourist = {
  113.                     name: name,
  114.                     possessions: []
  115.                 };
  116.  
  117.                 /**
  118.                  * Add to ihe collection of luggages the first luggage
  119.                  * this tourist has
  120.                  */
  121.                 tourist.possessions.push(possesion);
  122.  
  123.                 /**
  124.                  * Add this tourist to the collection of tourists
  125.                  */
  126.                 collection.push(tourist);
  127.             }
  128.         }
  129.  
  130.         /**
  131.          * Custom sorting function.
  132.          * It uses the criteria we have pop-ed from the array of args
  133.          * and use it as a key in the "possession" object.
  134.          * The "possession" object has "weight" and "luggagename" keys
  135.          *
  136.          * @param a Luggage/Possession object
  137.          * @param b Luggage/Possession object
  138.          * @returns int (-1, 0, 1)
  139.          */
  140.         function mySort(a, b)
  141.         {
  142.             /*
  143.                 Don't do anything if it's "strict"
  144.             */
  145.             if (criteria == 'strict') {
  146.                 return 0;
  147.             }
  148.  
  149.             return a[criteria] > b[criteria];
  150.         }
  151.  
  152.         collection.forEach(function(tourist) {
  153.             /**
  154.              * Sort with the custom function each tourist's luggage
  155.              */
  156.             tourist.possessions.sort(mySort);
  157.         });
  158.  
  159.         var result = {};
  160.  
  161.         /**
  162.          * Make from the collection of tourists the desired object
  163.          * for the output, with key name - the tourist name,
  164.          * and inner keys of each tourist - the luggage name.
  165.          */
  166.         collection.forEach(function(tourist) {
  167.             /**
  168.              * {"Yana" : {}};
  169.              */
  170.             result[tourist.name] = {};
  171.             tourist.possessions.forEach(function(possesion) {
  172.                 /**
  173.                  * {"Yana" : {"clothes" : { kg: *, fragile: *, type: *, transferredWith: * }};
  174.                  */
  175.                 result[tourist.name][possesion.luggagename] = {
  176.                     kg: possesion.weight,
  177.                     fragile: possesion.isFragile,
  178.                     type: possesion.type,
  179.                     transferredWith: possesion.transferredWith
  180.                 }
  181.             })
  182.         });
  183.  
  184.         console.log(JSON.stringify(result));
  185.     }
  186.  
  187.     solve ([
  188.         'Yana Slavcheva...*..clothes..*false*false......*...false..*..2.2..*.backpack',
  189.         'Kiko..*...socks..*false*false......*...false..*..0.2..*.backpack',
  190.         'Kiko....*...banana..*true*false......*...false..*..3.2..*.backpack',
  191.         'Kiko......*...sticks..*false.....*false......*...false..*..1.6..*.ATV',
  192.         'Kiko*...glasses..*false*...false......*...true..*..3..*.ATV',
  193.         'Manov..*...socks..*false*false....*...false..*0.3..*.ATV',
  194.         'luggage name']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement