Skip to content
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

Challenge: stazrouti submission #143

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
70 changes: 70 additions & 0 deletions submissions/stazrouti/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//to run the code use node solution.js in the terminal in this directory
// Importing the fs module for file operations
const fs = require('fs');

// Reading the input.txt file
fs.readFile('../../input.txt', 'utf8', (err, data) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loads the whole file in memory I believe. It can trigger ab OOM

Copy link
Author

@stazrouti stazrouti Feb 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aboullaite
but the memory usage is not indicated in the requirement !!

angry

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its 22G of data! There is no such thing as infinite memory

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aboullaite
but there is no need for a lot of ressources

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loading the whole file in memory would require 22G, plus the overhead of any data structure to handle your code, and then everything else. That's a lot of memory

Copy link
Author

@stazrouti stazrouti Feb 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aboullaite
sorry but no need for 22 G, i try it

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am no nodejs expert, but this article and many others point to that readFile load file in memory https://betterprogramming.pub/a-memory-friendly-way-of-reading-files-in-node-js-a45ad0cc7bb6

if (err) throw err;

// Splitting the data into lines
let lines = data.split('\n');

// Creating maps to store city data and product data
let cityData = new Map();
let productData = new Map();

// Variables to track the city with the smallest sum
let smallestCity = null;
let smallestSum = Infinity;

// Looping through each line of data
for (let i = 0; i < lines.length; i++) {
let [city, product, priceStr] = lines[i].split(',');
let price = parseFloat(priceStr);

// Checking if the price is valid and the product is not empty
if (!isNaN(price) && product !== '') {
// Updating the cityData map with the sum of prices for each city
cityData.set(city, (cityData.get(city) || 0) + price);

// Updating the productData map with the products and prices for each city
if (!productData.has(city)) {
productData.set(city, new Set());
}
productData.get(city).add({ product, price });
}
}

// Finding the city with the smallest sum
for (let [city, sum] of cityData) {
if (sum < smallestSum) {
smallestSum = sum;
smallestCity = city;
}
}

// Checking if a smallest city is found
if (smallestCity) {
// Sorting and filtering the products for the smallest city
let sortedProducts = Array.from(productData.get(smallestCity))
.sort((a, b) => a.price - b.price || a.product.localeCompare(b.product))
.reduce((acc, current) => {
if (!acc.some(x => x.product === current.product)) {
acc.push(current);
}
return acc;
}, [])
.slice(0, 6);

// Formatting the products and prices
let products = sortedProducts.map(({ product, price }) => `${product} ${price.toFixed(2)}`);

// Creating the output array
let outputArray = [`${smallestCity} ${smallestSum.toFixed(2)}`, ...products];

// Writing the output array to the output.txt file
fs.writeFile('../../output.txt', outputArray.join('\n'), (err) => {
if (err) throw err;
});
}
});