diff --git a/index.js b/index.js new file mode 100644 index 0000000..3b9e4fe --- /dev/null +++ b/index.js @@ -0,0 +1,13 @@ +const {problem1 } = require("./problem1.js"); +const {problem2 } = require("./problem2.js"); + +const Problem = async () => { + //Problem Solution 1 Call + // await problem1(); + + await problem2(); + await problem1(); + +}; + +Problem(); \ No newline at end of file diff --git a/output1.csv b/output1.csv index 7c7b275..99ae731 100644 --- a/output1.csv +++ b/output1.csv @@ -1,4 +1,4 @@ -D1,true ,P1,2000 -D2,true ,P1,3250 -D3,true ,P3,15300 +D1,true,P1,2000 +D2,true,P1,3250 +D3,true,P3,15300 D4,false,"","" diff --git a/output2.csv b/output2.csv index adcd15b..05e29c6 100644 --- a/output2.csv +++ b/output2.csv @@ -1,4 +1,5 @@ -D1,true ,P2,3000 -D2,true ,P1,3250 -D3,true ,P3,15300 +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..f5ebb99 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,264 @@ +{ + "name": "codechallenge", + "version": "1.0.0", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "codechallenge", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "csv-parser": "^3.0.0", + "csv-readable-stream": "^0.0.1", + "csv-reader": "^1.0.10", + "csvtojson": "^2.0.10" + } + }, + "node_modules/bluebird": { + "version": "3.7.2", + "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz", + "integrity": "sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==" + }, + "node_modules/core-util-is": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", + "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==" + }, + "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/csv-readable-stream": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/csv-readable-stream/-/csv-readable-stream-0.0.1.tgz", + "integrity": "sha512-yj7IF0oEKnaargh7VtRTGTOLOdwxK1oVrmZaXvAll4ZooqVelR3CK13fqBUoX83570reikFdr+JS/g3+qdF7tw==", + "dependencies": { + "readable-stream": "^2.0.4" + } + }, + "node_modules/csv-reader": { + "version": "1.0.10", + "resolved": "https://registry.npmjs.org/csv-reader/-/csv-reader-1.0.10.tgz", + "integrity": "sha512-4IgSQSwu/dCRjnrz+BnNe5LZiAHa2sOhccROI0r1gB1FW38Fl/k7Zx0jN3o77e4PTgd9T0cp1mgsD1esJNK+tQ==", + "engines": { + "node": ">=8.0.0" + } + }, + "node_modules/csvtojson": { + "version": "2.0.10", + "resolved": "https://registry.npmjs.org/csvtojson/-/csvtojson-2.0.10.tgz", + "integrity": "sha512-lUWFxGKyhraKCW8Qghz6Z0f2l/PqB1W3AO0HKJzGIQ5JRSlR651ekJDiGJbBT4sRNNv5ddnSGVEnsxP9XRCVpQ==", + "dependencies": { + "bluebird": "^3.5.1", + "lodash": "^4.17.3", + "strip-bom": "^2.0.0" + }, + "bin": { + "csvtojson": "bin/csvtojson" + }, + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + }, + "node_modules/is-utf8": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/is-utf8/-/is-utf8-0.2.1.tgz", + "integrity": "sha512-rMYPYvCzsXywIsldgLaSoPlw5PfoB/ssr7hY4pLfcodrA5M/eArza1a9VmTiNIBNMjOGr1Ow9mTyU2o69U6U9Q==" + }, + "node_modules/isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==" + }, + "node_modules/lodash": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==" + }, + "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/process-nextick-args": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==" + }, + "node_modules/readable-stream": { + "version": "2.3.7", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz", + "integrity": "sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==", + "dependencies": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, + "node_modules/string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "dependencies": { + "safe-buffer": "~5.1.0" + } + }, + "node_modules/strip-bom": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-2.0.0.tgz", + "integrity": "sha512-kwrX1y7czp1E69n2ajbG65mIo9dqvJ+8aBQXOGVxqwvNbsXdFM6Lq37dLAY3mknUwru8CfcCbfOLL/gMo+fi3g==", + "dependencies": { + "is-utf8": "^0.2.0" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" + } + }, + "dependencies": { + "bluebird": { + "version": "3.7.2", + "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz", + "integrity": "sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==" + }, + "core-util-is": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", + "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==" + }, + "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" + } + }, + "csv-readable-stream": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/csv-readable-stream/-/csv-readable-stream-0.0.1.tgz", + "integrity": "sha512-yj7IF0oEKnaargh7VtRTGTOLOdwxK1oVrmZaXvAll4ZooqVelR3CK13fqBUoX83570reikFdr+JS/g3+qdF7tw==", + "requires": { + "readable-stream": "^2.0.4" + } + }, + "csv-reader": { + "version": "1.0.10", + "resolved": "https://registry.npmjs.org/csv-reader/-/csv-reader-1.0.10.tgz", + "integrity": "sha512-4IgSQSwu/dCRjnrz+BnNe5LZiAHa2sOhccROI0r1gB1FW38Fl/k7Zx0jN3o77e4PTgd9T0cp1mgsD1esJNK+tQ==" + }, + "csvtojson": { + "version": "2.0.10", + "resolved": "https://registry.npmjs.org/csvtojson/-/csvtojson-2.0.10.tgz", + "integrity": "sha512-lUWFxGKyhraKCW8Qghz6Z0f2l/PqB1W3AO0HKJzGIQ5JRSlR651ekJDiGJbBT4sRNNv5ddnSGVEnsxP9XRCVpQ==", + "requires": { + "bluebird": "^3.5.1", + "lodash": "^4.17.3", + "strip-bom": "^2.0.0" + } + }, + "inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + }, + "is-utf8": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/is-utf8/-/is-utf8-0.2.1.tgz", + "integrity": "sha512-rMYPYvCzsXywIsldgLaSoPlw5PfoB/ssr7hY4pLfcodrA5M/eArza1a9VmTiNIBNMjOGr1Ow9mTyU2o69U6U9Q==" + }, + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==" + }, + "lodash": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==" + }, + "minimist": { + "version": "1.2.7", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.7.tgz", + "integrity": "sha512-bzfL1YUZsP41gmu/qjrEk0Q6i2ix/cVeAhbCbqH9u3zYutS1cLg00qhrD0M2MVdCcx4Sc0UpP2eBWo9rotpq6g==" + }, + "process-nextick-args": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==" + }, + "readable-stream": { + "version": "2.3.7", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz", + "integrity": "sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==", + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, + "string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "requires": { + "safe-buffer": "~5.1.0" + } + }, + "strip-bom": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-2.0.0.tgz", + "integrity": "sha512-kwrX1y7czp1E69n2ajbG65mIo9dqvJ+8aBQXOGVxqwvNbsXdFM6Lq37dLAY3mknUwru8CfcCbfOLL/gMo+fi3g==", + "requires": { + "is-utf8": "^0.2.0" + } + }, + "util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..c26651c --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "name": "codechallenge", + "version": "1.0.0", + "description": "Qube Cinemas Challenge 2019", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node index.js" + }, + "author": "Barath", + "license": "ISC", + "dependencies": { + "csv-parser": "^3.0.0", + "csv-readable-stream": "^0.0.1", + "csv-reader": "^1.0.10", + "csvtojson": "^2.0.10" + } +} diff --git a/parser.js b/parser.js new file mode 100644 index 0000000..a14bf74 --- /dev/null +++ b/parser.js @@ -0,0 +1,111 @@ + +const csv = require("csv-parser"); + +const fs = require("fs"); + +//stores the partner data +let results = []; +//store the input data +let input = []; +//stores the capacity data +let capacities = []; + +//This is used to parse the partner csv file into a array of object +/* +[{theater,startingslab,endingslab,minimumCost,costPerGB,partnerId},{...},{...}] +*/ +function partnerParse(filename) { + return new Promise((resolve, reject) => { + fs.createReadStream(filename) + .pipe(csv({ headers: false, skipLines: 1 })) + .on("data", (data) => { + try { + let slabSizeArray = data["1"].split("-"); + results.push({ + Theater: data["0"].trim(), + startingSlab: parseInt(slabSizeArray[0].trim()), + endingSlab: parseInt(slabSizeArray[1].trim()), + minimumCost: parseInt(data[2].trim()), + costPerGB: parseInt(data[3].trim()), + partnerID: data[4].trim(), + }); + } catch (error) { + console.log("error while parsing ", error); + } + }) + .on("end", () => { + resolve(results); + // console.log("results", results); + }) + .on("error", (err) => { + reject(err); + }); + }); +} + +//This is used to parse the input csv file into a array of object +/* +[{delivery,sizeUsed,theater},{...},{...}] +*/ +function inputParse(filename) { + return new Promise((resolve, reject) => { + fs.createReadStream(filename) + .pipe(csv({ headers: false })) + .on("data", (data) => { + try { + input.push({ + delivery: data["0"].trim(), + sizeUsed: parseInt(data["1"].trim()), + theater: data["2"].trim(), + }); + } catch (error) { + console.log("error while parsing ", error); + } + }) + .on("end", () => { + resolve(input); + // console.log("results", results); + }) + + .on("error", (err) => { + reject(err); + }); + }); +} + +//This is used to parse the capacity csv file into a array of object +/* +[{partnerID,capacity},{...},{...}] +*/ +function capacityParse(filename) { + return new Promise((resolve, reject) => { + fs.createReadStream(filename) + .pipe(csv({ headers: false, skipLines: 1 })) + .on("data", (data) => { + try { + // console.log("data", data); + capacities.push({ + partnerID: data["0"].trim(), + capacity: parseInt(data["1"].trim()), + }); + console.log("capacities", capacities); + } catch (error) { + console.log("error while parsing ", error); + } + }) + .on("end", () => { + resolve(capacities); + // console.log("results", results); + }) + + .on("error", (err) => { + reject(err); + }); + }); +} +//export the module that are in need for problem 1 and problem 2 +module.exports = { + partnerParse, + inputParse, + capacityParse, +}; diff --git a/problem1.js b/problem1.js new file mode 100644 index 0000000..3ed3b5b --- /dev/null +++ b/problem1.js @@ -0,0 +1,109 @@ +const fs = require("fs"); + +const { inputParse, partnerParse } = require("./parser.js"); + +//write a async function which calls parnterPatrse,inputParse in problem 1 and store in separate variables + +async function problem1() { + try { + let result = await partnerParse("partners.csv"); + let input = await inputParse("input.csv"); + //status variable to set the status + let status = false; + //output variable to push the output of problem 1 + let output = []; + + //Declare a for loop to iterate through the input array and result array + for (let i = 0; i < input.length; i++) { + for (let j = 0; j < result.length; j++) { + //condition to check the theater is present or not for the input with partner + /* + check the sizeUsed is between the slab + */ + if ( + input[i].theater === result[j].Theater && + input[i].sizeUsed >= result[j].startingSlab && + input[i].sizeUsed <= result[j].endingSlab + ) { + status = true; + //calculation for cost by multiplying the sizeused with cost per gb and store in variable. + let cost = input[i].sizeUsed * result[j].costPerGB; + + //if cost is less than minimum cost then assign the minimumCost to cost + if (cost < result[j].minimumCost) { + cost = result[j].minimumCost; + } + //Push the data to output with push function and assign the partner for every possible partner whose able to deliver + output.push({ + delivery: input[i].delivery, + status: status, + // sizeUsed: input[i].sizeUsed, + //theater: input[i].theater, + partnerID: result[j].partnerID, + cost: cost, + }); + } + } + } + //declared a for loop to seperate the input that is not in slab and given theater is not in input + + for (let i = 0; i < input.length; i++) { + for (let j = 0; j < output.length; j++) { + if ( + input[i].theater !== result[j].Theater && + input[i].sizeUsed <= result[j].startingSlab && + input[i].sizeUsed >= result[j].endingSlab + ) { + //output[j].theater = input[i].theater; + status = false; + } + } + if (status === false) { + output.push({ + delivery: input[i].delivery, + status: status, + //sizeUsed: input[i].sizeUsed, + //theater: input[i].theater, + partnerID: '""', + cost: '""', + }); + } + status = false; + } + + //sort the output array based on cost from low to high so we can filter the low cost for the delivery + output.sort((a, b) => a.cost - b.cost); + + // remove the duplicate values from the output array so there will be only one low cost delivery + output = output.filter( + (object, index, self) => + index === self.findIndex((t) => t.delivery === object.delivery) + ); + + //sort the delivery from d1 - d2 for better view + output.sort((a, b) => a.delivery[1] - b.delivery[1]); + + //write the finalData to output1.csv file using fs module and csv-writer module after erasing the previous data + + let finalData = output.map((item) => { + return Object.values(item); + }); + + //emptyb the output1 file + fs.writeFile("output1.csv", "", function (err) { + if (err) throw err; + console.log("File is created successfully."); + }); + + //append the finalData to output1.csv file by leaving a line after each row + for (let i = 0; i < finalData.length; i++) { + fs.appendFileSync("output1.csv", finalData[i] + "\n"); + } + } catch (error) { + console.log("error while calling partnerParse", error); + } +} + +module.exports = { + problem1 +}; diff --git a/problem2.js b/problem2.js new file mode 100644 index 0000000..056d3f6 --- /dev/null +++ b/problem2.js @@ -0,0 +1,111 @@ +//Import all the parse functions for data +const { inputParse, partnerParse,capacityParse } = require("./parser.js"); +const fs = require("fs"); + +async function problem2() { + try { + // Erasing the contents of output2.csv to add the new add data for clear representation. + fs.writeFile("output2.csv", "", (error) => { + if (error) throw error; + }); + + //declare a variable to store the parsed Data of partner file + const result = await partnerParse("partners.csv"); + + //declare a variable to store the parsed Data of input file + const input = await inputParse("input.csv"); + //declare a variable to store the parsed Data of capacities file + const capacities = await capacityParse("capacities.csv"); + console.log(result); + let partnerId = ""; + let Status = false; + let output = []; + let finalOutput = ""; + let finalCost = ""; + + //Sorting the input file based on the size used as gb + input.sort((a, b) => parseInt(b.sizeUsed) - parseInt(a.sizeUsed)); + //loop the input file data with input variable which as the data of inputb file + for (let i = 0; i < input.length; i++) { + //separate all the input data with filter function + let seperateTheatre = result.filter( + (value) => value.Theater == input[i].theater + ); + // loop the data of seperate data + for (let j = 0; j < seperateTheatre.length; j++) { + //seperate the capacity with filter function with partnerId of capacity and seperate theater + let capacity = capacities.filter( + (value) => value.partnerID == seperateTheatre[j].partnerID + )[0].capacity; + + //condition to check size used is in slab and capacity for that sizeused is greater to assign the partner that as more capacity than size used + if ( + input[i].sizeUsed <= seperateTheatre[j].endingSlab && + input[i].sizeUsed >= seperateTheatre[j].startingSlab && + capacity >= input[i].sizeUsed + ) { + Status = true; + + // declare a variable to store the cost that is multiply by costPerGb with size used.If cost is above minimum cost it will be set as cost else minimum cost will be assigned as cost + let cost = + input[i].sizeUsed * seperateTheatre[j].costPerGB > + seperateTheatre[j].minimumCost + ? input[i].sizeUsed * seperateTheatre[j].costPerGB + : seperateTheatre[j].minimumCost; + + if (finalCost != "") { + if (finalCost > cost) { + finalCost = cost; + partnerId = seperateTheatre[j].partnerID; + } + } else { + finalCost = cost; + partnerId = seperateTheatre[j].partnerID; + } + } + } + + //Checking if delivery is possible and updating the new capacity by subtracting the currently covered capacity + if (Status) { + let filteredCapArr = capacities.filter( + (value) => value.partnerID == partnerId + ); + for (let rows = 0; rows < filteredCapArr.length; rows++) { + filteredCapArr[rows].capacity = + filteredCapArr[rows].capacity - input[i].sizeUsed; + } + } + + // + output.push( + `${input[i].delivery},${Status},${partnerId != "" ? partnerId : '""'},${ + finalCost != "" ? finalCost : '""' + }` + ); + //Resetting the variables + finalCost = ""; + partnerId = ""; + Status = false; + + //Sorting the data in the array and storing it in another variable and appending to the output2.csv file. + output.sort(); + //Resetting each time to get a new row + finalOutput = ""; + for (let data = 0; data < output.length; data++) { + if (data == 0) { + finalOutput += `${output[data]} \n`; + } else { + finalOutput += `${output[data]}\n`; + } + } + } + // Appending the final array to output2.csv file + fs.appendFileSync("output2.csv", finalOutput + "\n"); + } + + catch (err) { + console.log("error while calling problem2", error); + } +} + +module.exports = { problem2 };