Skip to content

Solution for problem 1 is updated #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
44 changes: 1 addition & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
```
4 changes: 0 additions & 4 deletions capacities.csv

This file was deleted.

89 changes: 89 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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 = [];
});
})
}
4 changes: 0 additions & 4 deletions output1.csv

This file was deleted.

4 changes: 0 additions & 4 deletions output2.csv

This file was deleted.

64 changes: 64 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
89 changes: 89 additions & 0 deletions solutions.js
Original file line number Diff line number Diff line change
@@ -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
}