Skip to content
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
12 changes: 6 additions & 6 deletions output.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Sidi_Slimane 487539,37
Onion 1,01
Turnip 1,01
Salsify 1,04
Butternut_Squash 1,06
Clementine 1,06
Tetouan 48.50
gaz 1.30
potato 3.50
tomato 3.50
sugar 4.50
flour 5.20
79 changes: 79 additions & 0 deletions submissions/Ibourk-El/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const fs = require("fs");
const readline = require("readline");

const inputFile = fs.createReadStream("input.txt");
const outputFile = fs.createWriteStream("output.txt");

const bestCity = async () => {
try {
const rl = readline.createInterface({
input: inputFile,
crlfDelay: Infinity,
});
let cityData = {};
for await (const line of rl) {
if (line !== "") {
const [city, product, priceStr] = line.split(",");
const price = parseFloat(priceStr);
if (city in cityData) {
// check if the product is in list
if (product in cityData[city].product) {
let ind = cityData[city].product[product];
// change the value of the product to cheapes one
if (cityData[city].price[ind].price > price) {
cityData[city].price[ind].price = price;
}
} else {
// add new product
cityData[city].product[product] = cityData[city].index;
cityData[city].price.push({ name: product, price: price });
cityData[city].index += 1;
}
cityData[city].sum += price;
} else {
// add new city
cityData[city] = {
product: {},
price: [{ name: product, price: price }],
sum: price,
index: 1,
};
cityData[city].product[product] = 0;
}
}
}

let smallestSum = Infinity;
let bestCity = "";
for (let city in cityData) {
if (cityData[city].sum < smallestSum) {
smallestSum = cityData[city].sum;
bestCity = city;
}
}

cityData[bestCity].price.sort((a, b) => {
if (a.price === b.price) {
if (a.name > b.name) return 1;
else if (a.name < b.name) return -1;
else return 0;
}
return a.price - b.price;
});

let result = `${bestCity} ${smallestSum.toFixed(2)} \n`;
let i = 0;
while (i < 5) {
let t = cityData[bestCity].price[i].name;
result += t + " " + cityData[bestCity].price[i].price.toFixed(2) + "\n";
i++;
}

outputFile.write(result);
outputFile.end();
} catch (e) {
console.log(e);
}
};

bestCity();
70 changes: 0 additions & 70 deletions submissions/Touil-Ali/index.js

This file was deleted.

65 changes: 0 additions & 65 deletions submissions/aboullaite/Main.java

This file was deleted.

86 changes: 0 additions & 86 deletions submissions/conan/Main.java

This file was deleted.