-
Notifications
You must be signed in to change notification settings - Fork 159
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
stazrouti
wants to merge
1
commit into
geeksblabla:main
Choose a base branch
from
stazrouti:blanat-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) => { | ||
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; | ||
}); | ||
} | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 !!
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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