Skip to content

Instantly share code, notes, and snippets.

@clarkenheim
clarkenheim / Zip Codes to DMAs
Last active November 13, 2024 03:00
TSV file containing zip codes and the DMA they fall in to. Method: calculate the centre point of every zip code geo boundary, plot those points on a DMA boundary map, find the containing DMA of each zip centroid
This file has been truncated, but you can view the full file.
zip_code dma_code dma_description
01001 543 SPRINGFIELD - HOLYOKE
01002 543 SPRINGFIELD - HOLYOKE
01003 543 SPRINGFIELD - HOLYOKE
01004 543 SPRINGFIELD - HOLYOKE
01005 506 BOSTON (MANCHESTER)
01007 543 SPRINGFIELD - HOLYOKE
01008 543 SPRINGFIELD - HOLYOKE
01009 543 SPRINGFIELD - HOLYOKE
01010 543 SPRINGFIELD - HOLYOKE
@clarkenheim
clarkenheim / mongodb_distinct_count.js
Last active December 12, 2023 09:22
MongoDB equivalent of an SQL query to get the distinct values of a field in a collection including the count of documents which have each distinct value (distinct with count)
//equivalent of MySQL SELECT COUNT(*) AS cnt, fieldName FROM someTable GROUP BY fieldName;
db.someCollection.aggregate([{"$group" : {_id:"$fieldName", cnt:{$sum:1}}}]);
//as above but ordered by the count descending
//eg: SELECT COUNT(*) AS cnt, fieldName FROM someTable GROUP BY fieldName ORDER BY cnt DESC;
db.someCollection.aggregate([{"$group" : {_id:"$fieldName", cnt:{$sum:1}}}, {$sort:{'cnt':-1}}]);
@clarkenheim
clarkenheim / 99bottles.js
Created January 3, 2013 09:52
The song "99 bottles of beer" in 2 lines of javascript
var i=99,w=" on the wall",t=" bottle",o=" of beer";
for(;i;)b=i+(i^1?t+'s':t)+o,console.log(b+w+", "+b+".\nTake one down and pass it around, "+--i+(i^1?t+'s':t)+w)