Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* The distance between Anchova Bichkiya Hut and Momina Polyana Hut is 3 hours in walking.
- All the people that wanted to spend the New Year's Eve there must transfer their luggage to Momina Polyana Hut –
- all the food, all the drinks, everything. The real tourists transferred their stuff in their backpacks.
- The lazy ones did that by using an ATV machine.
- Your task is to give information about the luggage to everybody that asks you, because you are the leader
- of the group.
- Fortunately everybody writes down everything about their own luggage on sheets of paper.
- You are given a list of luggage names on separate sheets given as text table with the following columns:
- [owner name].*.[luggage name] .*. [is food] .*.[is drink] .*.[is fragile] .*.[weight in kg] .*.[transferred with].
- You are given sorting criterion as a string on the last line of the text table. The different columns
- will always be separated by exactly one '*' sign and 0 or more '.' sign on the left and 0 or more '.' sign on
- the right side of the '*' sign (for example valid separators are: '*', '.*', '*…….', '...*…..').
- The [owner name] and [luggage name] are strings. The [is food], [is drink], [is fragile] are strings -
- either 'true' or 'false'. If 'is food' is 'true', the type of the luggage is 'food'. If 'is drink' is 'true',
- the type of the luggage is 'drink' ('is drink' and 'is food' cannot both be 'true'). If 'is food' and 'is drink'
- are false, the type of the luggage is 'other'. The [weight] is floating-point number.
- The [transferred with] is a string – either 'backpack' or 'ATV'.
- The sorting criterion can be 'luggage name', 'weight' or 'strict'.
- Aggregate the results and print them as JSON string. The order of the owner names is the order of appearance
- in the input. The order of the luggage pieces is according to the sorting criterion.
- If the sorting criterion is 'luggage name', the luggage pieces are ordered alphabetically by their name.
- If the sorting criterion is 'weight', the luggage pieces are ordered by their weight in ascending order.
- If the sorting criterion is 'strict', the luggage pieces are ordered by their order of appearance in the input. */
- "use strict";
- function solve(args) {
- var luggage = {};
- var sortCriterion = args[args.length - 1];
- for (var i = 0; i < args.length - 1; i += 1) {
- var inputLine = args[i].split(/\.*\*\.*/g);
- var ownerName = inputLine[0];
- var luggageName = inputLine[1];
- var luggageType = (inputLine[2] == 'true') ? 'food' : (inputLine[3] == 'true') ? 'drink' : 'other';
- var fragille = (inputLine[4] == 'true');
- if (!(ownerName in luggage)) {
- luggage[ownerName] = {};
- }
- luggage[ownerName][luggageName] = {
- 'kg': parseFloat(inputLine[5]),
- 'fragile': fragille,
- 'type': luggageType,
- 'transferredWith': inputLine[6]
- };
- }
- if (sortCriterion != 'strict') {
- luggage = sortingObject(luggage, sortCriterion);
- }
- console.log(JSON.stringify(luggage));
- function sortingObject(object, criterion) {
- var newObject = {};
- Object.keys(object).forEach(function (obj) {
- newObject[obj] = {};
- var sorted = Object.keys(object[obj]);
- if (criterion == 'luggage name') {
- sorted.sort();
- } else if (criterion == 'weight') {
- sorted.sort(function (a, b) {
- return object[obj][a].kg - object[obj][b].kg;
- });
- }
- sorted.forEach(function (key) {
- newObject[obj][key] = object[obj][key];
- });
- });
- return newObject;
- }
- }
- solve([
- 'Yana Slavcheva.*.clothes.*.false.*.false.*.false.*.2.2.*.backpack',
- 'Kiko.*.socks.*.false.*.false.*.false.*.0.2.*.backpack',
- 'Kiko.*.banana.*.true.*.false.*.false.*.3.2.*.backpack',
- 'Kiko.*.sticks.*.false.*.false.*.false.*.1.6.*.ATV',
- 'Kiko.*.glasses.*.false.*.false.*.true.*.3.*.ATV',
- 'Manov.*.socks.*.false.*.false.*.false.*.0.3.*.ATV',
- 'strict'
- ]);
- solve([
- 'Yana Slavcheva.*.clothes.*.false.*.false.*.false.*.2.2.*.backpack',
- 'Kiko.*.socks.*.false.*.false.*.false.*.0.2.*.backpack',
- 'Kiko.*.banana.*.true.*.false.*.false.*.3.2.*.backpack',
- 'Kiko.*.sticks.*.false.*.false.*.false.*.1.6.*.ATV',
- 'Kiko.*.glasses.*.false.*.false.*.true.*.3.*.ATV',
- 'Manov.*.socks.*.false.*.false.*.false.*.0.3.*.ATV',
- 'weight'
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement