From 43363ee5800d9f1bff52fed2b2fe0a0f73646303 Mon Sep 17 00:00:00 2001 From: Deva45anbu Date: Thu, 27 Oct 2022 13:59:44 +0530 Subject: [PATCH] Solution for problem 1 is updated --- .gitignore | 1 + README.md | 44 +---------------------- capacities.csv | 4 --- index.js | 89 +++++++++++++++++++++++++++++++++++++++++++++++ output1.csv | 4 --- output2.csv | 4 --- package-lock.json | 64 ++++++++++++++++++++++++++++++++++ package.json | 15 ++++++++ solutions.js | 89 +++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 259 insertions(+), 55 deletions(-) create mode 100644 .gitignore delete mode 100644 capacities.csv create mode 100644 index.js delete mode 100644 output1.csv delete mode 100644 output2.csv create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 solutions.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..30bc162 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/node_modules \ No newline at end of file diff --git a/README.md b/README.md index b162885..ba3f5df 100644 --- a/README.md +++ b/README.md @@ -71,46 +71,4 @@ D2, 300, T1 ``` D1, false, "", "" D2, true, P1, 4500 -``` - -## Problem Statement 2 - -Each partner specifies the **maximum capacity** they can serve, across all their deliveries in following manner: - -Table 2: - -| Partner ID | Capacity (in GB) | -| ------------- |:----------------:| -| P1 | 500 | -| P2 | 300 | - -We have provided `capacities.csv` which contain ID and capacities for each partner. - -Given a list of content size and Theatre ID, Assign deliveries to partners in such a way that all deliveries are possible (Higher Priority) and overall cost of delivery is minimum (i.e. First make sure no delivery is impossible and then minimise the sum of cost of all the delivery). If delivery is not possible to a theatre, mark that delivery impossible. Take partner capacity into consideration as well. - -Use `partners.csv` and `capacities.csv`. - -**Input**: Same as Problem statement 1. - -**Expected Output**: Same as Problem statement 1. - -#### Sample Scenario (Based on above table 1 and 2): -**INPUT**: -``` -D1, 100, T1 -D2, 240, T1 -D3, 260, T1 -``` - -**OUTPUT**: -``` -D1, true, P2, 2500 -D2, true, P1, 3600 -D3, true, P1, 3900 -``` - -**Explanation**: Only partner P1 and P2 can deliver content to T1. Lowest cost of delivery will be achieved if all three deliveries are given to partner P1 (100\*20+240\*15+260\*15 = 9,500). However, P1 has capacity of 500 GB and total assigned capacity is (100+240+260) 600 GB in this case. Assigning any one of the delivery to P2 will bring the capacity under 500. Assigning the D1, D2 and D3 to P2 is increasing the total cost of delivery by 500 (100\*25+240\*15+260\*15-9500), 2400 (100\*20+240\*25+260*15-9500) and 2600 (100\*20+240\*15+260\*25-9500) respectively. Hence, Assigning D1 to P2. - -To submit a solution, fork this repo and send a Pull Request on Github. - -For any questions or clarifications, raise an issue on this repo and we'll answer your questions as fast as we can. +``` \ No newline at end of file diff --git a/capacities.csv b/capacities.csv deleted file mode 100644 index ae622cf..0000000 --- a/capacities.csv +++ /dev/null @@ -1,4 +0,0 @@ -"Partner ID","Capacity (in GB)" -P1 ,350 -P2 ,500 -P3 ,1500 diff --git a/index.js b/index.js new file mode 100644 index 0000000..26df84f --- /dev/null +++ b/index.js @@ -0,0 +1,89 @@ +const fs = require('fs') + +const csv = require('csv-parser'); +// import the functions problem1Solution from solution.js +const solutions = require('./solutions.js'); +let tempData = []; + +// call the main function to execute the solution +main() +async function main() { + try { + console.log('inside main') + let partnerData = await parsePartnerCSV(); + console.log("partnerData holds data from partner.csv \n", partnerData) + let inputData = await parseInputCSV(); + console.log("inputData holds data from input.csv ", inputData) + + // problem1Solution is called to execute solution for problem1 with partnerData and inputData as input + solutions.problem1Solution(partnerData, inputData) + + + } + catch (error) { + console.log(error) + } + +} + +// parsePartnerCSV function is to read data from partners.csv +function parsePartnerCSV() { + return new Promise((resolve, reject) => { + fs.createReadStream(__dirname + '/partners.csv') + .pipe(csv({ headers: false, skipLines: 1 })) + .on('data', function (data) { + try { + + partnerTemp = { + "theaterId": data[0].trim(), + "minSlabSize": parseInt(data[1].split('-')[0].trim()), + "maxSlabSize": parseInt(data[1].split('-')[1].trim()), + "minimumCost": parseInt(data[2].trim()), + "costPerGb": parseInt(data[3].trim()), + "partnerId": data[4].trim() + } + tempData.push(partnerTemp) + // console.log(partnerTemp) + } + catch (err) { + console.error("Error parsing csv file : " + err) + reject(err) + } + }) + .on('end', function () { + + resolve(tempData) + tempData = []; + }); + }) +} + + +// parsePartnerCSV function is to read data from input.csv +function parseInputCSV() { + return new Promise((resolve, reject) => { + fs.createReadStream(__dirname + '/input.csv') + .pipe(csv({ headers: false })) + .on('data', function (data) { + try { + + inputTemp = { + "deliveryId": data[0].trim(), + "gbSize": parseInt(data[1].trim()), + "theaterId": data[2].trim(), + } + tempData.push(inputTemp) + // console.log(partnerTemp) + } + catch (err) { + console.error("Error parsing csv file : " + err) + reject(err) + } + }) + .on('end', function () { + + resolve(tempData) + tempData = []; + }); + }) +} \ No newline at end of file diff --git a/output1.csv b/output1.csv deleted file mode 100644 index 7c7b275..0000000 --- a/output1.csv +++ /dev/null @@ -1,4 +0,0 @@ -D1,true ,P1,2000 -D2,true ,P1,3250 -D3,true ,P3,15300 -D4,false,"","" diff --git a/output2.csv b/output2.csv deleted file mode 100644 index adcd15b..0000000 --- a/output2.csv +++ /dev/null @@ -1,4 +0,0 @@ -D1,true ,P2,3000 -D2,true ,P1,3250 -D3,true ,P3,15300 -D4,false,"","" diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..32b2ca2 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,64 @@ +{ + "name": "qube_cinemas_challenge_2019", + "version": "1.0.0", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "qube_cinemas_challenge_2019", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "csv-parser": "^3.0.0", + "random-words": "^1.2.0" + } + }, + "node_modules/csv-parser": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/csv-parser/-/csv-parser-3.0.0.tgz", + "integrity": "sha512-s6OYSXAK3IdKqYO33y09jhypG/bSDHPuyCme/IdEHfWpLf/jKcpitVFyOC6UemgGk8v7Q5u2XE0vvwmanxhGlQ==", + "dependencies": { + "minimist": "^1.2.0" + }, + "bin": { + "csv-parser": "bin/csv-parser" + }, + "engines": { + "node": ">= 10" + } + }, + "node_modules/minimist": { + "version": "1.2.7", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.7.tgz", + "integrity": "sha512-bzfL1YUZsP41gmu/qjrEk0Q6i2ix/cVeAhbCbqH9u3zYutS1cLg00qhrD0M2MVdCcx4Sc0UpP2eBWo9rotpq6g==", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/random-words": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/random-words/-/random-words-1.2.0.tgz", + "integrity": "sha512-YP2bXrT19pxtBh22DK9CLcWsmBjUBAGzw3JWJycTNbXm1+0aS6PrKuAJ9aLT0GGaPlPp9LExfJIMVkzhrDZE6g==" + } + }, + "dependencies": { + "csv-parser": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/csv-parser/-/csv-parser-3.0.0.tgz", + "integrity": "sha512-s6OYSXAK3IdKqYO33y09jhypG/bSDHPuyCme/IdEHfWpLf/jKcpitVFyOC6UemgGk8v7Q5u2XE0vvwmanxhGlQ==", + "requires": { + "minimist": "^1.2.0" + } + }, + "minimist": { + "version": "1.2.7", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.7.tgz", + "integrity": "sha512-bzfL1YUZsP41gmu/qjrEk0Q6i2ix/cVeAhbCbqH9u3zYutS1cLg00qhrD0M2MVdCcx4Sc0UpP2eBWo9rotpq6g==" + }, + "random-words": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/random-words/-/random-words-1.2.0.tgz", + "integrity": "sha512-YP2bXrT19pxtBh22DK9CLcWsmBjUBAGzw3JWJycTNbXm1+0aS6PrKuAJ9aLT0GGaPlPp9LExfJIMVkzhrDZE6g==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..dddf814 --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "qube_cinemas_challenge_2019", + "version": "1.0.0", + "description": "to complete problem1 and problem2 statements", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Deva Anbu G", + "license": "ISC", + "dependencies": { + "csv-parser": "^3.0.0", + "random-words": "^1.2.0" + } +} diff --git a/solutions.js b/solutions.js new file mode 100644 index 0000000..2df1de5 --- /dev/null +++ b/solutions.js @@ -0,0 +1,89 @@ +const fs = require('fs') + +async function problem1Solution(partnerData, inputData) { + console.log('inside problem1Solution') + // ref.p1.1 declare a variable to store output + // ref.p1.2 declare a temporary object to store output data for each input + // ref.p1.3 loop the input data + // ref.p1.4 loop the partner data + // ref.p1.5 assign delivery id to the output object + // ref.p1.6 check to get the partner data for the current theater id + // ref.p1.7 check to get the input gb size lies between the partner min and max slab size and set delivery status as true + // ref.p1.8 find the product of input gb size and cost per gb in current partner data + // ref.p1.9 if the product is less than the current partner's minimum cost assign the input cost to minimum cost else assign the product + // ref.p1.10 find then minimum cost found for the input and set it to a temporary object + // ref.p1.11 push the temporary object to output1 array + // ref.p1.12 reset the temporary object + // ref.p1.13 create a new csv output1.csv + // ref.p1.14 push the output1array to output1.csv + + + // ref.p1.1 + let output1 = []; + // ref.p1.2 + let tempObject = { + deliveryId: '', + deliveryStatus: '', + finalCost: -1, + partnerId: '', + } + + + //ref.p1.3 + for (let inputIndex = 0; inputIndex < inputData.length; inputIndex++) { + //ref.p1.4 + for (let parentIndex = 0; parentIndex < partnerData.length; parentIndex++) { + // ref.p1.5 + tempObject.deliveryId = inputData[inputIndex].deliveryId + // ref.p1.6,ref.p1.7 + if (inputData[inputIndex].theaterId == partnerData[parentIndex].theaterId && + (inputData[inputIndex].gbSize >= partnerData[parentIndex].minSlabSize && inputData[inputIndex].gbSize <= partnerData[parentIndex].maxSlabSize + )) { + tempObject.deliveryStatus = true + // ref.p1.8, ref.p1.9 + totalCost = inputData[inputIndex].gbSize * partnerData[parentIndex].costPerGb > partnerData[parentIndex].minimumCost + ? inputData[inputIndex].gbSize * partnerData[parentIndex].costPerGb + : partnerData[parentIndex].minimumCost; + // ref.p1.10 + if (tempObject.finalCost == -1) { + tempObject.finalCost = totalCost, + tempObject.partnerId = partnerData[parentIndex].partnerId + } + else if (tempObject.finalCost > totalCost) { + tempObject.finalCost = totalCost, + tempObject.partnerId = partnerData[parentIndex].partnerId + } + // console.log(finalCost) + } + } + console.log(tempObject) + // ref.p1.11 + if (inputIndex == 0) { + output1.push(tempObject.deliveryId, tempObject.deliveryStatus, tempObject.partnerId, tempObject.finalCost == -1 ? '' : tempObject.finalCost) + } + else { + output1.push('\n' + tempObject.deliveryId, tempObject.deliveryStatus, tempObject.partnerId, tempObject.finalCost == -1 ? '' : tempObject.finalCost) + } + // ref.p1.12 + tempObject.deliveryStatus = false + tempObject.finalCost = -1 + tempObject.partnerId = '' + } + console.log(output1) + // ref.p1.13 + fs.writeFile("output1.csv", "", (error) => { + if (error) throw error; + }); + // ref.p1.14 + fs.appendFileSync("output1.csv", output1 + "\n"); +} + + + + + + +// export problem1Solution function +module.exports = { + problem1Solution +} \ No newline at end of file