Created
April 4, 2020 23:44
-
-
Save binodpanta/78407dea21dd239fd6fbf4788f3bc859 to your computer and use it in GitHub Desktop.
solution for localytics
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
const path = require('path'); | |
const fs = require('fs'); | |
const _ = require('lodash'); | |
// | |
// salesMap : categoryMap = { category: [ price1, price2 ] } | |
// salesMap = { category: [ avgSalesPrice ] } | |
// | |
/** | |
* @param {Array} categoriesRawData input array of categories [ { item: name, category: cat } ] | |
* @returns {object} { productName: category } | |
*/ | |
const prodToCategory = (categoriesRawData) => { | |
const categoryMap = new Map(); | |
categoriesRawData.map(itemData => { | |
const prodName = itemData.item; | |
if (prodName) | |
categoryMap.set(prodName, itemData.category); | |
}); | |
return categoryMap; | |
}; | |
const categoriesRawDataFromFile = fs.readFileSync(path.resolve('products.tab'), { encoding: 'utf-8' }); | |
const splitByLine = categoriesRawDataFromFile.split("\r\n"); | |
const categoriesRawData = _.map(splitByLine, line => { | |
const splitup = line.split('\t'); | |
return { item: splitup[0], category: splitup[1] || "Uncategorized" }; | |
}); | |
const categoryMap = prodToCategory(categoriesRawData); | |
// categoryMap.forEach(item => console.log(item)); | |
// go over all prices and assign [price1, price2] to a map { category: prices } | |
const pricesRawDataFromFile = fs.readFileSync(path.resolve('sales.tab'), { encoding: 'utf-8' }); | |
const pricesSplitByLine = pricesRawDataFromFile.split("\n"); | |
const categoryToPricesMap = new Map(); | |
_.map(_.filter(pricesSplitByLine, l => l && l.trim().length > 1), line => { | |
const splitup = line.split('\t'); | |
const item = splitup[0]; | |
const price = splitup[1]; | |
const categoryFromMap = categoryMap.get(item); | |
const category = categoryFromMap || 'Uncategorized'; | |
if (item && price) { | |
if (categoryToPricesMap.has(category)) { | |
const currentItem = categoryToPricesMap.get(category); | |
categoryToPricesMap.set(category, _.concat(currentItem, Number.parseFloat(price))); | |
} else { | |
categoryToPricesMap.set(category, [ Number.parseFloat(price) ]); | |
} | |
} else { | |
throw new Error(`${line}`); | |
} | |
}); | |
const mapOfAverages = new Map(); | |
categoryToPricesMap.forEach((value, key, map) => { | |
const avg = _.mean(value); | |
mapOfAverages.set(key, avg); | |
}); | |
// print all averages by category | |
mapOfAverages.forEach(( item, key ) => console.log(`${key} avg: ${item}`)); | |
// print category with max price | |
const maxPriceItem = _.maxBy(Array.from(mapOfAverages), item => item[1]); | |
console.log(`\n\n"${maxPriceItem[0]}" has the highest average sales price of ${maxPriceItem[1]}\n\n`); | |
// print category breakfast sale prices | |
const breakFastPrices = categoryToPricesMap.get("Breakfast"); | |
const maxPrice = _.max(breakFastPrices); | |
const minPrice = _.min(breakFastPrices); | |
console.log(`Max breakfast price ${maxPrice}\nMin breakfast price ${minPrice}`); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment