Skip to content

Rohit Nandakumar-Coding Assignment #70

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 2 commits 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
28 changes: 28 additions & 0 deletions 2020-Freshers/goodie-dilemma/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const prompt=require("prompt-sync")();
var fs = require('fs');
var input = fs.readFileSync('input.txt').toString().split('\r\n'); //Read file from input.txt
var inputObj = {}; //Convert to object
input.forEach(function(line) {
var lineArr = line.split(':');
inputObj[lineArr[0]] = Number(lineArr[1]);
});
const sortArray=Object.entries(inputObj).sort((a,b)=>{ //Convert object to array and sorting it in ascending order
return a[1]-b[1];
})
var emp=Number(prompt("Enter the number of employees "))
var min=sortArray[emp-1][1]-sortArray[0][1]
var minIndex=0
//Working below to find the minimum difference between goodies
for(var i=1;i<sortArray.length-emp+1;i++){
if(sortArray[i+emp-1][1]-sortArray[i][1]<min){
min=sortArray[i+emp-1][1]-sortArray[i][1]
minIndex=i
}
}
const result=sortArray.slice(minIndex,minIndex+emp).map((item)=>{
return `\n${item[0]}:${item[1]}`
}).join(' ')
const data=`The goodies selected for distribution are: \n${result}\n\nAnd the difference between the chosen goodie with highest price and the lowest price is ${min}`
console.log(data)
fs.writeFileSync("output.txt",data) //Write the output into output.txt file
console.log(data)
10 changes: 10 additions & 0 deletions 2020-Freshers/goodie-dilemma/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Fitbit Plus:7980
Ipods:22349
MI Band:999
Cult Pass:2799
Macbook Pro:229900
Digital Camera:11101
Alexa:9999
Sandwich Toaster:2195
Microwave Oven:9800
Scale:4999
6 changes: 6 additions & 0 deletions 2020-Freshers/goodie-dilemma/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
The goodies selected for distribution are:

Microwave Oven:9800
Alexa:9999

And the difference between the chosen goodie with highest price and the lowest price is 199
14 changes: 0 additions & 14 deletions 2020-Freshers/goodie-dilemma/sample_input.txt

This file was deleted.

8 changes: 0 additions & 8 deletions 2020-Freshers/goodie-dilemma/sample_output.txt

This file was deleted.