From 0d23487b8086e11277a326a97c3a7e0e4ab66941 Mon Sep 17 00:00:00 2001 From: Barath-Kumar-99 <83857364+Barath-Kumar-99@users.noreply.github.com> Date: Sun, 23 Oct 2022 12:23:30 +0530 Subject: [PATCH 01/10] Add files via upload --- problem1.js | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 problem1.js diff --git a/problem1.js b/problem1.js new file mode 100644 index 0000000..bfd6e76 --- /dev/null +++ b/problem1.js @@ -0,0 +1,106 @@ +const fs = require("fs"); +const { inputParse, partnerParse } = require("./parser.js"); + +//write a async function which calls the above function and returns the result to index.js + +async function problem1() { + try { + let result = await partnerParse("partners.csv"); + let input = await inputParse("input.csv"); + let status = false; + let output = []; + console.log("helllllll00", input, result); + + //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++) { + if ( + input[i].theater === result[j].Theater && + input[i].sizeUsed >= result[j].startingSlab && + input[i].sizeUsed <= result[j].endingSlab + ) { + status = true; + let cost = input[i].sizeUsed * result[j].costPerGB; + if (cost < result[j].minimumCost) { + cost = result[j].minimumCost; + } + + output.push({ + delivery: input[i].delivery, + status: status, + // sizeUsed: input[i].sizeUsed, + //theater: input[i].theater, + partnerID: result[j].partnerID, + cost: cost, + }); + console.log("output1112", output); + } + + console.log("output22221", output); + } + } + + 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 + output.sort((a, b) => a.cost - b.cost); + + // remove the duplicate values from the output array + output = output.filter( + (object, index, self) => + index === self.findIndex((t) => t.delivery === object.delivery) + ); + //write a condition to push the data which is not present in slab range + + 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); + }); + + console.log("finalData", finalData); + fs.writeFile("output1.csv", "", function (err) { + if (err) throw err; + console.log("File is created successfully."); + }); + // fs.appendFileSync("output1.csv", finalData + "\n\n"); + //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"); + } + + // console.log("output1", output); + } catch (error) { + console.log("error while calling partnerParse", error); + } +} + +module.exports = { + problem1, + partnerParse, + inputParse, +}; From 134b0711d90d6d8b23d6f4c0328c1d907077e916 Mon Sep 17 00:00:00 2001 From: Barath-Kumar-99 <83857364+Barath-Kumar-99@users.noreply.github.com> Date: Sun, 23 Oct 2022 12:23:56 +0530 Subject: [PATCH 02/10] Add files via upload --- output1.csv | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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,"","" From 03040ea556100e0114dece5d97199c7ba7ce8377 Mon Sep 17 00:00:00 2001 From: Barath-Kumar-99 <83857364+Barath-Kumar-99@users.noreply.github.com> Date: Sun, 23 Oct 2022 12:24:50 +0530 Subject: [PATCH 03/10] Add files via upload --- output2.csv | 8 ++-- parser.js | 93 +++++++++++++++++++++++++++++++++++++++++++++++ problem2.js | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 200 insertions(+), 4 deletions(-) create mode 100644 parser.js create mode 100644 problem2.js diff --git a/output2.csv b/output2.csv index adcd15b..35e6fa0 100644 --- a/output2.csv +++ b/output2.csv @@ -1,4 +1,4 @@ -D1,true ,P2,3000 -D2,true ,P1,3250 -D3,true ,P3,15300 -D4,false,"","" +D1,true ,P2,3000 +D2,true ,P1,3250 +D3,true ,P3,15300 +D4,false,"","" diff --git a/parser.js b/parser.js new file mode 100644 index 0000000..5365307 --- /dev/null +++ b/parser.js @@ -0,0 +1,93 @@ +const csv = require("csv-parser"); + +const fs = require("fs"); +let results = []; +let input = []; +let capacities = []; +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); + }); + }); +} + +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); + }); + }); +} + +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); + }); + }); +} + +module.exports = { + partnerParse, + inputParse, + capacityParse, +}; diff --git a/problem2.js b/problem2.js new file mode 100644 index 0000000..18d9436 --- /dev/null +++ b/problem2.js @@ -0,0 +1,103 @@ +//import the fs +const fs = require("fs"); +// import partnerParse function from problem1.js +const { partnerParse, inputParse, capacityParse } = require("./problem1.js"); +//import inputParse function from problem1.js + +// create a async function which calls the above functions and returns the result to index.js +async function problem2() { + try { + let result = await partnerParse("partners.csv"); + let input = await inputParse("input.csv"); + let capacitydata = await capacityParse("capacities.csv"); + let status = false; + let output = []; + // console.log("capacitydata", capacitydata); + //sort the input array based on sizeUsed + input.sort((a, b) => b.sizeUsed - a.sizeUsed); + // console.log("input", input); + + //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++) { + //create a let variable to store the capacity of the partner and minus the sizeUsed from it + + // + + if ( + input[i].theater === result[j].Theater && + input[i].sizeUsed >= result[j].startingSlab && + input[i].sizeUsed <= result[j].endingSlab + // capacitydata[j].capacity >= input[i].sizeUsed + ) { + status = true; + let cost = input[i].sizeUsed * result[j].costPerGB; + if (cost < result[j].minimumCost) { + cost = result[j].minimumCost; + } + + output.push({ + delivery: input[i].delivery, + status: status, + sizeUsed: input[i].sizeUsed, + theater: input[i].theater, + partnerID: result[j].partnerID, + cost: cost, + }); + } + } + } + + // capacity.sort((a, b) => b.capacities - a.capacities); + // console.log("capacity", capacity); + + // // declare a for loop to iterate through the output array and capacity array and check the capacity of the partner + // for (let i = 0; i < output.length; i++) { + // for (let j = 0; j < capacity.length; j++) { + + // } + // } + + // console.log("output", output); + // console.log("capacitydata11", capacitydata); + // create a for loop to iterate output and capacity array and check the capacity of the partner and minus the sizeUsed from it and push the result to output array + + //console.log("output111", output); + + //sort the output array based on cost + output.sort((a, b) => a.cost - b.cost); + + // remove the duplicate values from the output array + output = output.filter( + (object, index, self) => + index === self.findIndex((t) => t.delivery === object.delivery) + ); + //write a condition to push the data which is not present in slab range + + output.sort((a, b) => a.delivery[1] - b.delivery[1]); + console.log("output", output); + + //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); + }); + + // console.log("finalData", finalData); + fs.writeFile("output2.csv", "", function (err) { + if (err) throw err; + console.log("File is created successfully."); + }); + // fs.appendFileSync("output1.csv", finalData + "\n\n"); + //append the finalData to output1.csv file by leaving a line after each row + for (let i = 0; i < finalData.length; i++) { + fs.appendFileSync("output2.csv", finalData[i] + "\n"); + } + + //console.log("capacities", capacity); + } catch (error) { + console.log("error while calling problem 2", error); + } +} + +module.exports = { problem2 }; From 26f5dbfc45d2ba49492311cca2ab243273c5eee7 Mon Sep 17 00:00:00 2001 From: Barath-Kumar-99 <83857364+Barath-Kumar-99@users.noreply.github.com> Date: Sun, 23 Oct 2022 12:37:18 +0530 Subject: [PATCH 04/10] Add files via upload --- package-lock.json | 264 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 18 ++++ 2 files changed, 282 insertions(+) create mode 100644 package-lock.json create mode 100644 package.json 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" + } +} From 30310b8a906671735bd6d8c759d9df111010782a Mon Sep 17 00:00:00 2001 From: Barath-Kumar-99 <83857364+Barath-Kumar-99@users.noreply.github.com> Date: Sun, 23 Oct 2022 13:50:33 +0530 Subject: [PATCH 05/10] Add files via upload --- index.js | 6 ++ parser.js | 204 ++++++++++++++++++++++++++---------------------- problem1.js | 217 +++++++++++++++++++++++++++------------------------- 3 files changed, 228 insertions(+), 199 deletions(-) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..ca1fe50 --- /dev/null +++ b/index.js @@ -0,0 +1,6 @@ +const {problem1 } = require("./problem1.js"); +const { capactityParse, problem2 } = require("./problem2.js"); + + +//This funtion will gets called first +problem1(); diff --git a/parser.js b/parser.js index 5365307..a14bf74 100644 --- a/parser.js +++ b/parser.js @@ -1,93 +1,111 @@ -const csv = require("csv-parser"); - -const fs = require("fs"); -let results = []; -let input = []; -let capacities = []; -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); - }); - }); -} - -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); - }); - }); -} - -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); - }); - }); -} - -module.exports = { - partnerParse, - inputParse, - capacityParse, -}; + +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 index bfd6e76..c9b797b 100644 --- a/problem1.js +++ b/problem1.js @@ -1,106 +1,111 @@ -const fs = require("fs"); -const { inputParse, partnerParse } = require("./parser.js"); - -//write a async function which calls the above function and returns the result to index.js - -async function problem1() { - try { - let result = await partnerParse("partners.csv"); - let input = await inputParse("input.csv"); - let status = false; - let output = []; - console.log("helllllll00", input, result); - - //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++) { - if ( - input[i].theater === result[j].Theater && - input[i].sizeUsed >= result[j].startingSlab && - input[i].sizeUsed <= result[j].endingSlab - ) { - status = true; - let cost = input[i].sizeUsed * result[j].costPerGB; - if (cost < result[j].minimumCost) { - cost = result[j].minimumCost; - } - - output.push({ - delivery: input[i].delivery, - status: status, - // sizeUsed: input[i].sizeUsed, - //theater: input[i].theater, - partnerID: result[j].partnerID, - cost: cost, - }); - console.log("output1112", output); - } - - console.log("output22221", output); - } - } - - 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 - output.sort((a, b) => a.cost - b.cost); - - // remove the duplicate values from the output array - output = output.filter( - (object, index, self) => - index === self.findIndex((t) => t.delivery === object.delivery) - ); - //write a condition to push the data which is not present in slab range - - 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); - }); - - console.log("finalData", finalData); - fs.writeFile("output1.csv", "", function (err) { - if (err) throw err; - console.log("File is created successfully."); - }); - // fs.appendFileSync("output1.csv", finalData + "\n\n"); - //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"); - } - - // console.log("output1", output); - } catch (error) { - console.log("error while calling partnerParse", error); - } -} - -module.exports = { - problem1, - partnerParse, - inputParse, -}; +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, + partnerParse, + inputParse, +}; From 5cf97851fccdd8480673aee39e4ee08dca6a676e Mon Sep 17 00:00:00 2001 From: Barath-Kumar-99 <83857364+Barath-Kumar-99@users.noreply.github.com> Date: Sun, 23 Oct 2022 20:52:50 +0530 Subject: [PATCH 06/10] Add files via upload --- problem2.js | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/problem2.js b/problem2.js index 18d9436..0d0ef68 100644 --- a/problem2.js +++ b/problem2.js @@ -1,3 +1,108 @@ +<<<<<<< HEAD +//import the fs +const fs = require("fs"); +// import partnerParse function from problem1.js +const { partnerParse, inputParse, capacityParse } = require("./problem1.js"); +//import inputParse function from problem1.js + +// create a async function which calls the above functions and returns the result to index.js +async function problem2() { + try { + let result = await partnerParse("partners.csv"); + let input = await inputParse("input.csv"); + let capacitydata = await capacityParse("capacities.csv"); + let status = false; + let output = []; + // console.log("capacitydata", capacitydata); + //sort the input array based on sizeUsed + input.sort((a, b) => b.sizeUsed - a.sizeUsed); + // console.log("input", input); + + //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++) { + //create a let variable to store the capacity of the partner and minus the sizeUsed from it + + // + + if ( + input[i].theater === result[j].Theater && + input[i].sizeUsed >= result[j].startingSlab && + input[i].sizeUsed <= result[j].endingSlab + // capacitydata[j].capacity >= input[i].sizeUsed + ) { + status = true; + let cost = input[i].sizeUsed * result[j].costPerGB; + if (cost < result[j].minimumCost) { + cost = result[j].minimumCost; + } + + output.push({ + delivery: input[i].delivery, + status: status, + sizeUsed: input[i].sizeUsed, + theater: input[i].theater, + partnerID: result[j].partnerID, + cost: cost, + }); + } + } + } + + // capacity.sort((a, b) => b.capacities - a.capacities); + // console.log("capacity", capacity); + + // // declare a for loop to iterate through the output array and capacity array and check the capacity of the partner + // for (let i = 0; i < output.length; i++) { + // for (let j = 0; j < capacity.length; j++) { + + // } + // } + + // console.log("output", output); + // console.log("capacitydata11", capacitydata); + // create a for loop to iterate output and capacity array and check the capacity of the partner and minus the sizeUsed from it and push the result to output array + + //console.log("output111", output); + + //sort the output array based on cost + output.sort((a, b) => a.cost - b.cost); + + // remove the duplicate values from the output array + output = output.filter( + (object, index, self) => + index === self.findIndex((t) => t.delivery === object.delivery) + ); + //write a condition to push the data which is not present in slab range + + output.sort((a, b) => a.delivery[1] - b.delivery[1]); + console.log("output", output); + + //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); + }); + + // console.log("finalData", finalData); + fs.writeFile("output2.csv", "", function (err) { + if (err) throw err; + console.log("File is created successfully."); + }); + // fs.appendFileSync("output1.csv", finalData + "\n\n"); + //append the finalData to output1.csv file by leaving a line after each row + for (let i = 0; i < finalData.length; i++) { + fs.appendFileSync("output2.csv", finalData[i] + "\n"); + } + + //console.log("capacities", capacity); + } catch (error) { + console.log("error while calling problem 2", error); + } +} + +module.exports = { problem2 }; +======= //import the fs const fs = require("fs"); // import partnerParse function from problem1.js @@ -101,3 +206,4 @@ async function problem2() { } module.exports = { problem2 }; +>>>>>>> 03040ea556100e0114dece5d97199c7ba7ce8377 From e8e3a94ce4a59d9caf66b4a1365f1d5e94881f98 Mon Sep 17 00:00:00 2001 From: Barath-Kumar-99 <83857364+Barath-Kumar-99@users.noreply.github.com> Date: Sun, 23 Oct 2022 20:59:32 +0530 Subject: [PATCH 07/10] Add files via upload --- index.js | 5 +- output2.csv | 9 +- problem2.js | 282 +++++++++++++++++----------------------------------- 3 files changed, 99 insertions(+), 197 deletions(-) diff --git a/index.js b/index.js index ca1fe50..15a9b5c 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,5 @@ const {problem1 } = require("./problem1.js"); -const { capactityParse, problem2 } = require("./problem2.js"); +const {problem2 } = require("./problem2.js"); - -//This funtion will gets called first problem1(); +problem2(); \ No newline at end of file diff --git a/output2.csv b/output2.csv index 35e6fa0..0e7d348 100644 --- a/output2.csv +++ b/output2.csv @@ -1,4 +1,5 @@ -D1,true ,P2,3000 -D2,true ,P1,3250 -D3,true ,P3,15300 -D4,false,"","" +D4,false,"","" +D3,true,P3,15300 +D2,true,P1,3250 +D1,true,P2,3000 + diff --git a/problem2.js b/problem2.js index 0d0ef68..056d3f6 100644 --- a/problem2.js +++ b/problem2.js @@ -1,209 +1,111 @@ -<<<<<<< HEAD -//import the fs -const fs = require("fs"); -// import partnerParse function from problem1.js -const { partnerParse, inputParse, capacityParse } = require("./problem1.js"); -//import inputParse function from problem1.js - -// create a async function which calls the above functions and returns the result to index.js -async function problem2() { - try { - let result = await partnerParse("partners.csv"); - let input = await inputParse("input.csv"); - let capacitydata = await capacityParse("capacities.csv"); - let status = false; - let output = []; - // console.log("capacitydata", capacitydata); - //sort the input array based on sizeUsed - input.sort((a, b) => b.sizeUsed - a.sizeUsed); - // console.log("input", input); - - //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++) { - //create a let variable to store the capacity of the partner and minus the sizeUsed from it - - // - - if ( - input[i].theater === result[j].Theater && - input[i].sizeUsed >= result[j].startingSlab && - input[i].sizeUsed <= result[j].endingSlab - // capacitydata[j].capacity >= input[i].sizeUsed - ) { - status = true; - let cost = input[i].sizeUsed * result[j].costPerGB; - if (cost < result[j].minimumCost) { - cost = result[j].minimumCost; - } - - output.push({ - delivery: input[i].delivery, - status: status, - sizeUsed: input[i].sizeUsed, - theater: input[i].theater, - partnerID: result[j].partnerID, - cost: cost, - }); - } - } - } - - // capacity.sort((a, b) => b.capacities - a.capacities); - // console.log("capacity", capacity); - - // // declare a for loop to iterate through the output array and capacity array and check the capacity of the partner - // for (let i = 0; i < output.length; i++) { - // for (let j = 0; j < capacity.length; j++) { - - // } - // } - - // console.log("output", output); - // console.log("capacitydata11", capacitydata); - // create a for loop to iterate output and capacity array and check the capacity of the partner and minus the sizeUsed from it and push the result to output array - - //console.log("output111", output); - - //sort the output array based on cost - output.sort((a, b) => a.cost - b.cost); - - // remove the duplicate values from the output array - output = output.filter( - (object, index, self) => - index === self.findIndex((t) => t.delivery === object.delivery) - ); - //write a condition to push the data which is not present in slab range - - output.sort((a, b) => a.delivery[1] - b.delivery[1]); - console.log("output", output); - - //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); - }); - - // console.log("finalData", finalData); - fs.writeFile("output2.csv", "", function (err) { - if (err) throw err; - console.log("File is created successfully."); - }); - // fs.appendFileSync("output1.csv", finalData + "\n\n"); - //append the finalData to output1.csv file by leaving a line after each row - for (let i = 0; i < finalData.length; i++) { - fs.appendFileSync("output2.csv", finalData[i] + "\n"); - } - - //console.log("capacities", capacity); - } catch (error) { - console.log("error while calling problem 2", error); - } -} - -module.exports = { problem2 }; -======= -//import the fs +//Import all the parse functions for data +const { inputParse, partnerParse,capacityParse } = require("./parser.js"); const fs = require("fs"); -// import partnerParse function from problem1.js -const { partnerParse, inputParse, capacityParse } = require("./problem1.js"); -//import inputParse function from problem1.js -// create a async function which calls the above functions and returns the result to index.js async function problem2() { try { - let result = await partnerParse("partners.csv"); - let input = await inputParse("input.csv"); - let capacitydata = await capacityParse("capacities.csv"); - let status = false; - let output = []; - // console.log("capacitydata", capacitydata); - //sort the input array based on sizeUsed - input.sort((a, b) => b.sizeUsed - a.sizeUsed); - // console.log("input", input); + // 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 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++) { - //create a let variable to store the capacity of the partner and minus the sizeUsed from it + //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].theater === result[j].Theater && - input[i].sizeUsed >= result[j].startingSlab && - input[i].sizeUsed <= result[j].endingSlab - // capacitydata[j].capacity >= input[i].sizeUsed + input[i].sizeUsed <= seperateTheatre[j].endingSlab && + input[i].sizeUsed >= seperateTheatre[j].startingSlab && + capacity >= input[i].sizeUsed ) { - status = true; - let cost = input[i].sizeUsed * result[j].costPerGB; - if (cost < result[j].minimumCost) { - cost = result[j].minimumCost; + 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; } - - output.push({ - delivery: input[i].delivery, - status: status, - sizeUsed: input[i].sizeUsed, - theater: input[i].theater, - partnerID: result[j].partnerID, - cost: cost, - }); } } - } - - // capacity.sort((a, b) => b.capacities - a.capacities); - // console.log("capacity", capacity); - - // // declare a for loop to iterate through the output array and capacity array and check the capacity of the partner - // for (let i = 0; i < output.length; i++) { - // for (let j = 0; j < capacity.length; j++) { - - // } - // } - - // console.log("output", output); - // console.log("capacitydata11", capacitydata); - // create a for loop to iterate output and capacity array and check the capacity of the partner and minus the sizeUsed from it and push the result to output array - - //console.log("output111", output); - //sort the output array based on cost - output.sort((a, b) => a.cost - b.cost); - - // remove the duplicate values from the output array - output = output.filter( - (object, index, self) => - index === self.findIndex((t) => t.delivery === object.delivery) - ); - //write a condition to push the data which is not present in slab range - - output.sort((a, b) => a.delivery[1] - b.delivery[1]); - console.log("output", output); - - //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); - }); + //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; + } + } - // console.log("finalData", finalData); - fs.writeFile("output2.csv", "", function (err) { - if (err) throw err; - console.log("File is created successfully."); - }); - // fs.appendFileSync("output1.csv", finalData + "\n\n"); - //append the finalData to output1.csv file by leaving a line after each row - for (let i = 0; i < finalData.length; i++) { - fs.appendFileSync("output2.csv", finalData[i] + "\n"); + // + 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`; + } + } } - - //console.log("capacities", capacity); - } catch (error) { - console.log("error while calling problem 2", error); + // 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 }; ->>>>>>> 03040ea556100e0114dece5d97199c7ba7ce8377 From 3c0bb6b41692b1f6064f4a25e140ed8b823b5604 Mon Sep 17 00:00:00 2001 From: Barath-Kumar-99 <83857364+Barath-Kumar-99@users.noreply.github.com> Date: Sun, 23 Oct 2022 21:18:38 +0530 Subject: [PATCH 08/10] Add files via upload --- output2.csv | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/output2.csv b/output2.csv index 0e7d348..05e29c6 100644 --- a/output2.csv +++ b/output2.csv @@ -1,5 +1,5 @@ -D4,false,"","" -D3,true,P3,15300 +D1,true,P2,3000 D2,true,P1,3250 -D1,true,P2,3000 +D3,true,P3,15300 +D4,false,"","" From 3d67b9efeee52b246b95b6088f9dbddc795117de Mon Sep 17 00:00:00 2001 From: Barath-Kumar-99 <83857364+Barath-Kumar-99@users.noreply.github.com> Date: Wed, 26 Oct 2022 12:23:27 +0530 Subject: [PATCH 09/10] Add files via upload --- index.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 15a9b5c..3b9e4fe 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,13 @@ const {problem1 } = require("./problem1.js"); const {problem2 } = require("./problem2.js"); -problem1(); -problem2(); \ No newline at end of file +const Problem = async () => { + //Problem Solution 1 Call + // await problem1(); + + await problem2(); + await problem1(); + +}; + +Problem(); \ No newline at end of file From bf91b87b759bc494a9dd6420b6f2fca9adc1ee6d Mon Sep 17 00:00:00 2001 From: Barath-Kumar-99 <83857364+Barath-Kumar-99@users.noreply.github.com> Date: Wed, 26 Oct 2022 12:26:21 +0530 Subject: [PATCH 10/10] Update problem1.js --- problem1.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/problem1.js b/problem1.js index c9b797b..3ed3b5b 100644 --- a/problem1.js +++ b/problem1.js @@ -105,7 +105,5 @@ async function problem1() { } module.exports = { - problem1, - partnerParse, - inputParse, + problem1 };