Skip to content

Instantly share code, notes, and snippets.

View omgimanerd's full-sized avatar

Alvin Lin omgimanerd

View GitHub Profile
@omgimanerd
omgimanerd / CustomObjectSerialization.ts
Last active December 31, 2022 03:53
CustomObjectSerialization.ts for medium blog post
/**
* @fileoverview Returns a JSON replacer and reviver function that allows for
* registered classes to be serialized and deserialized from JSON objects.
*/
const __type = '__type'
const TYPENAME_MAP = 'Map'
type SerializableTypes = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
We can make this file beautiful and searchable if this error is corrected: Unclosed quoted field in line 7.
"PCT","CRIME","2000","2001","2002","2003","2004","2005","2006","2007","2008","2009","2010","2011","2012","2013","2014","2015","2016","2017","2018","2019"
"1","MISDEMEANOR POSSESSION OF STOLEN PROPERTY ","6","16","7","11","7","18","13","14","25","19","34","33","42","43","37","18","7","13","14","5"
"","MISDEMEANOR SEX CRIMES (4)","43","47","38","38","32","25","32","26","25","31","27","50","44","42","43","35","53","72","88","81"
"","MISDEMEANOR DANGEROUS DRUGS (1)","453","438","330","219","86","42","50","38","69","68","78","91","72","84","93","98","215","165","44","32"
"","MISDEMEANOR DANGEROUS WEAPONS (5) ","13","15","19","21","11","16","17","26","30","27","39","29","32","19","32","26","22","22","17","12"
"","PETIT LARCENY ","2617","2293","1884","1731","1852","1925","1851","1957","1948","1970","1702","1938","2119","2014","1863","1782","2014","1932","2537","2324"
"","ASSAULT 3 AND RELATED OFFENSES ","464","355","346","388","340","326","308","350","306","28
@omgimanerd
omgimanerd / field.py
Last active June 6, 2019 03:42
tetris-field-scorer.py
def get_scoring_vector(self):
"""
Get a vector of values derived from the field used to score a tetromino
placement.
"""
heights = self.heights()
ediff1d = np.ediff1d(heights)
return np.array([
self.count_gaps(), # Gap count
np.mean(heights), # Average height
@omgimanerd
omgimanerd / audio-processor-server.js
Last active February 12, 2018 23:07
Used in my Medium post: How To Build An Audio Processor In Your Browser
/** Server app script */
const PORT = 5000
// Dependencies.
const express = require('express')
const fs = require('fs')
const path = require('path')
const youtubeAudioStream = require('youtube-audio-stream')
@omgimanerd
omgimanerd / example-process-audio.js
Last active February 12, 2018 22:41
Used in my Medium post: How To Build An Audio Processor In Your Browser
/** Example processAudio() function */
const processAudio = (buffer) => {
// Create offline context
const offlineAudioContext = new OfflineAudioContext(
1, buffer.length, buffer.sampleRate)
// Create buffer source
const bufferSource = offlineAudioContext.createBufferSource()
bufferSource.buffer = buffer
@omgimanerd
omgimanerd / audio-processor-server.js
Last active February 12, 2018 22:44
Used in my Medium post: How To Build An Audio Processor In Your Browser
/** Express server app */
const PORT = 5000
// Dependencies.
const express = require('express')
const path = require('path')
const youtubeAudioStream = require('youtube-audio-stream')
const app = express()
@omgimanerd
omgimanerd / kmeans_native.py
Last active February 12, 2018 22:41
The KMeans clustering algorithm implemented using both numpy and native Python
#!/usr/bin/env python3
# Kmeans clustering algorithm heavily influenced by
# https://gist.github.com/iandanforth/5862470
import random
def get_color_distance(rgb1, rgb2):
"""
Given two RGB color values, this returns the squared euclidean distance
between the two colors.
@omgimanerd
omgimanerd / typeracer_2.py
Last active May 1, 2017 00:51
Used in my Medium post: How To Mess With TypeRacer
#!/usr/bin/env python3
import pyautogui
import time
print("Enter words:")
words = input()
time.sleep(2)
pyautogui.typewrite(words, interval=0.05);
@omgimanerd
omgimanerd / typeracer_1.js
Created April 30, 2017 09:00
Used in my Medium post: How To Mess With TypeRacer
var out = function() {
var firstWord = "";
var text = "";
for (var i = 0; i < 9999; ++i) {
var firstWordElement = $(`#nhwMiddlegwt-uid-${i}`);
if (firstWordElement) {
firstWord = firstWordElement.innerHTML;
}
var textElement = $(`#nhwRightgwt-uid-${i}`);
if (textElement) {
@omgimanerd
omgimanerd / auth_route.js
Last active April 10, 2017 04:02
Used in my Medium post: Automating My Personal Note-Taking System
var bodyParser = require('body-parser');
var crypto = require('crypto');
app.use(bodyParser.urlencoded({
extended: true,
verify: function(request, response, buffer, encoding) {
request.receivedHash = request.headers['x-hub-signature'];
request.computedHash = 'sha1=' + crypto.createHmac('sha1',
process.env.GITHUB_WEBHOOK_SECRET).update(buffer).digest('hex');
}