Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(args) {
  2.     let N = +args[0];
  3.     let M = +args[N + 1]
  4.     let pricesList = {};
  5.  
  6.     //fill pricesList
  7.     for (let i = 1; i < N + 1; i += 1) {
  8.         let prices = args[i].trim().split(' '),
  9.             len = prices.length,
  10.             currPrice = +prices[len - 1],
  11.             currProduct = prices[0];
  12.         for (let j = 1; j < len - 1; j += 1) {
  13.             currProduct += ' ' + prices[j];
  14.         }
  15.         pricesList[currProduct] = currPrice;
  16.     }
  17.  
  18.     let buyersList = args.slice(args.length - M)
  19.         .map(x => x.trim().split(','));
  20.  
  21.     for (let i = 0; i < buyersList.length; i += 1) {
  22.         let quantityProduct = {};
  23.         let total = 0;
  24.         for (let pr of buyersList[i]) {
  25.             let line = pr.trim().split(' ');
  26.             let quantity = 0;
  27.             if (!isNaN(+line[0])) {
  28.                 quantity += +line[0];
  29.                 let product = line[1];
  30.                 for (let j = 2; j < line.length; j += 1) {
  31.                     product += ' ' + line[j];
  32.                 }
  33.                 if (quantityProduct.hasOwnProperty(product)) {
  34.                     quantityProduct[product] += quantity;
  35.                 } else {
  36.                     quantityProduct[product] = quantity;
  37.                 }
  38.             } else {
  39.                 let product = line[0];
  40.                 quantity += 1;
  41.                 for (let j = 1; j < line.length; j += 1) {
  42.                     product += ' ' + line[j];
  43.                 }
  44.                 if (quantityProduct.hasOwnProperty(product)) {
  45.                     quantityProduct[product] += quantity;
  46.                 } else {
  47.                     quantityProduct[product] = quantity;
  48.                 }
  49.             }
  50.         }
  51.         for(let product in quantityProduct){
  52.             total += (quantityProduct[product] * pricesList[product]);
  53.         }
  54.         console.log(total.toFixed(2));
  55.  
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement