Skip to content

Instantly share code, notes, and snippets.

View convex_hull.py
import random
"""
Computes the Convex Hull with the Graham Scan algorithm
Use:
h = ConvexHull()
print(h.hull)
"""
View sudoku_solver.py
from random import shuffle
import copy
"""
SudokuGenerator
input: grid can be a 2-D matrix of a Sudoku puzzle to solve, or None to generate a new puzzle.
"""
View text_normalizer.py
import re
import string
import unidecode
from nltk.tokenize import sent_tokenize
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
from nltk.corpus import stopwords
import gensim.downloader as api
from pycontractions import Contractions
from word2number import w2n
@lvngd
lvngd / rectCollide.js
Created Feb 3, 2021
Rectangular collision detection in D3 force layouts. Blog post: https://lvngd.com/blog/rectangular-collision-detection-d3-force-layouts
View rectCollide.js
function rectCollide() {
var nodes,sizes,masses;
var strength = 1;
var iterations = 1;
var nodeCenterX;
var nodeMass;
var nodeCenterY;
function force() {
@lvngd
lvngd / d3_pictogram_demo_circles.html
Created Nov 20, 2020
How to build a pictogram grid in D3.js. Demo with SVG circles.
View d3_pictogram_demo_circles.html
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
<script src="http://d3js.org/d3.v4.js"></script>
</head>
<body>
<div id='grid-container'>
<div id='grid-chart'></div>
@lvngd
lvngd / rectCollide.js
Created Sep 22, 2021
rectCollide for D3 V6
View rectCollide.js
function rectCollide() {
var nodes,sizes,masses;
var strength = 1;
var iterations = 1;
var nodeCenterX;
var nodeMass;
var nodeCenterY;
function force() {
View albumYear.js
let yearAlbum = {};
let yearCount = new Map();
for (let i = 0; i < data.length; i++) {
for (let j = 0; j < data[i].bandAlbums.length; j++) {
if (!yearCount.has(data[i].bandAlbums[j].albumYear)) {
yearCount.set(`${data[i].bandAlbums[j].albumYear}`, 1)
} else {
yearCount.set(`${data[i].bandAlbums[j].albumYear}`, yearCount.get(`${data[i].bandAlbums[j].albumYear}`) + 1)
}
@lvngd
lvngd / createTables.js
Last active Mar 25, 2021
Node + Knex code to create postgres tables for darklyrics data
View createTables.js
/*
1st create database:
-go into the interactive interpreter
psql postgres
Here we're creating:
1. a databased called 'darklyrics'
2. a user 'darklyricsuser'
3. with password 'darklyricspassword'
@lvngd
lvngd / jsonToPostgres.js
Last active Mar 25, 2021
Code to insert darklyrics JSON data into postgres using Node + Knex
View jsonToPostgres.js
const knex = require('knex')({
client: 'pg',
connection: 'postgresql://darklyricsuser:darklyricspassword@localhost/darklyrics',
});
const fs = require('fs');
const data = JSON.parse(fs.readFileSync('/Users/christina/Downloads/results(1).json', 'utf8'));
async function insertBand(band) {
@lvngd
lvngd / d3_random_subsets.js
Created Mar 10, 2021
Demo of D3 data joins - displaying random subsets from a grid of circles.
View d3_random_subsets.js
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v6.min.js"></script>
</head>
<body>
<div id="chart"></div>
</body>