Last active
March 16, 2017 16:19
-
-
Save devNoiseConsulting/2d6737aa0a2f7411794e04d9e6916355 to your computer and use it in GitHub Desktop.
Best price per square foot - PhillyDev Slack #daily_programmer - 201703
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
/* | |
Download the data file from: | |
https://github.com/aisflat439/dailyProgrammer/blob/master/data/realEstate.csv | |
0 street | |
1 city | |
2 zip | |
3 state | |
4 beds | |
5 baths | |
6 sq__ft | |
7 type | |
8 sale_date | |
9 price | |
10 latitude | |
11 longitude | |
*/ | |
let fs = require('fs'); | |
const fileName = process.argv[2]; | |
let listings = readFile(fileName); | |
const minLat = listings.reduce(reduceMinLat, listings[0][10]); | |
const maxLat = listings.reduce(reduceMaxLat, listings[0][10]); | |
const minLng = listings.reduce(reduceMinLng, listings[0][11]); | |
const maxLng = listings.reduce(reduceMaxLng, listings[0][11]); | |
const midLat = (minLat + maxLat) / 2; | |
const midLng = (minLng + maxLng) / 2; | |
// Some listings have a price that is to low and must be the rental price. | |
// So I've decided that they should be omitted. | |
listings = listings.filter(minPrice); | |
const bestListing = { | |
"overall": listings.reduce(bestPrice, listings[0]), | |
"northern": listings.filter(isNorthern).reduce(bestPrice, listings[0]), | |
"southern": listings.filter(isSouthern).reduce(bestPrice, listings[0]), | |
"eatern": listings.filter(isEastern).reduce(bestPrice, listings[0]), | |
"western": listings.filter(isWestern).reduce(bestPrice, listings[0]), | |
}; | |
for(var key in bestListing) { | |
bestListing[key] = addPricePerSqFoot(bestListing[key]); | |
} | |
console.log(bestListing); | |
function bestPrice(best, listing) { | |
const oldPrice = pricePerSqFoot(best[9], best[6]); | |
const newPrice = pricePerSqFoot(listing[9], listing[6]); | |
return (oldPrice < newPrice) ? best : listing; | |
} | |
function pricePerSqFoot(price, squareFoot) { | |
//return (squareFoot > 0) ? price / squareFoot : 0; | |
return price / squareFoot; | |
} | |
function addPricePerSqFoot(listing) { | |
listing[12] = pricePerSqFoot(listing[9], listing[6]); | |
return listing; | |
} | |
function minPrice(listing) { | |
return listing[9] > 9999; | |
} | |
function isNorthern(listing) { | |
return listing[10] > midLat; | |
} | |
function isSouthern(listing) { | |
return listing[10] < midLat; | |
} | |
function isEastern(listing) { | |
return listing[11] > midLng; | |
} | |
function isWestern(listing) { | |
return listing[11] < midLng; | |
} | |
function reduceMinLat(a, b) { | |
return (a < b[10]) ? a : b[10]; | |
} | |
function reduceMaxLat(a, b) { | |
return (a > b[10]) ? a : b[10]; | |
} | |
function reduceMinLng(a, b) { | |
return (a < b[11]) ? a : b[11]; | |
} | |
function reduceMaxLng(a, b) { | |
return (a > b[11]) ? a : b[11]; | |
} | |
function readFile(fileName) { | |
let data; | |
try { | |
const text = fs.readFileSync(fileName, "utf8"); | |
data = text.split('\r'); | |
data = data.map(function(line) { | |
line = line.split(','); | |
// I need to use these columns as numbers and converting here to prevent problems elsewhere. | |
line[9] = parseInt(line[9]); | |
line[10] = parseFloat(line[10]); | |
line[11] = parseFloat(line[11]); | |
return line; | |
}); | |
// Clean up data, remove header row and empty last line. | |
data.shift(); | |
data.pop(); | |
} catch (e) { | |
console.log("ERROR: file: " + fileName + " error: " + e.message); | |
return false; | |
} | |
return data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment