Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(args) {
- let N = +args[0];
- let M = +args[N + 1]
- let pricesList = {};
- //fill pricesList
- for (let i = 1; i < N + 1; i += 1) {
- let prices = args[i].trim().split(' '),
- len = prices.length,
- currPrice = +prices[len - 1],
- currProduct = prices[0];
- for (let j = 1; j < len - 1; j += 1) {
- currProduct += ' ' + prices[j];
- }
- pricesList[currProduct] = currPrice;
- }
- let buyersList = args.slice(args.length - M)
- .map(x => x.trim().split(','));
- for (let i = 0; i < buyersList.length; i += 1) {
- let quantityProduct = {};
- let total = 0;
- for (let pr of buyersList[i]) {
- let line = pr.trim().split(' ');
- let quantity = 0;
- if (!isNaN(+line[0])) {
- quantity += +line[0];
- let product = line[1];
- for (let j = 2; j < line.length; j += 1) {
- product += ' ' + line[j];
- }
- if (quantityProduct.hasOwnProperty(product)) {
- quantityProduct[product] += quantity;
- } else {
- quantityProduct[product] = quantity;
- }
- } else {
- let product = line[0];
- quantity += 1;
- for (let j = 1; j < line.length; j += 1) {
- product += ' ' + line[j];
- }
- if (quantityProduct.hasOwnProperty(product)) {
- quantityProduct[product] += quantity;
- } else {
- quantityProduct[product] = quantity;
- }
- }
- }
- for(let product in quantityProduct){
- total += (quantityProduct[product] * pricesList[product]);
- }
- console.log(total.toFixed(2));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement