Skip to content

Instantly share code, notes, and snippets.

View scwood's full-sized avatar

Spencer Wood scwood

View GitHub Profile
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"
integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN"
crossorigin="anonymous">
<link
rel="stylesheet"
href="https://ca1.qualtrics.com/apps/applied-predictive-technologies/bootstrap_custom.css">
<style>
@scwood
scwood / uuidV4.js
Last active November 20, 2025 15:37
UUID v4 implementation in JavaScript
/*
Paraphrased from RFC 4122:
The formal definition of the UUID string representation is
provided by the following ABNF:
UUID = time-low "-" time-mid "-"
time-high-and-version "-"
clock-seq-and-reserved
clock-seq-low "-" node
@scwood
scwood / paint_bucket.py
Last active June 5, 2018 22:06
Paint bucket problem
def paint_bucket(image, row, col, new_color):
original_color = image[row][col]
_paint(image, row, col, new_color, original_color)
def _paint(image, row, col, new_color, original_color):
if (row < 0 or
row > len(image) - 1 or
col < 0 or
col > len(image[row]) - 1 or
image[row][col] != original_color):
@scwood
scwood / HashTable.js
Last active March 2, 2018 21:16
Hash table implementation in JavaScript with linear probing
class HashTable {
constructor() {
this.table = [];
this.length = 0;
this.maxLength = 8;
this.maxLoadFactor = 0.75;
}
set(key, value) {
@scwood
scwood / fuzzy.js
Last active February 23, 2018 19:56
Fuzzy Search in JS
const search = 'fiouedblds';
const strings = ['something random', '_filterOutNonEditableFields', 'abc', 'fiouedblds'];
function findFuzzyMatches(strings, search) {
return strings.filter((string) => isFuzzyMatch(string, search));
}
function isFuzzyMatch(string, search) {
string = string.toLowerCase();
search = search.toLowerCase();
@scwood
scwood / quicksort.js
Last active April 16, 2018 19:46
In place quicksort in JavaScript
function quickSort(arr) {
return quickSortAuxillary(arr, 0, arr.length - 1);
}
function quickSortAuxillary(arr, left, right) {
if (left < right) {
const middle = partition(arr, left, right);
quickSortAuxillary(arr, left, middle - 1);
quickSortAuxillary(arr, middle + 1, right);
}
@scwood
scwood / one_to_many_join.sql
Last active January 13, 2018 00:20
Join one to many relationship, select one row per the one on some criteria
-- one customer to many transactions
-- want to select most recent transaction for each customer
select * from customers join transactions on transactions.id = (
select id from transactions
where transactions.customer_id = customers.id
order by created_at desc
limit 1
)
@scwood
scwood / ssh_proxy_with_docker.bash
Created December 13, 2017 17:48
SSH proxy and access from inside Docker container
# On local machine
ssh -NL 9000:some-restricted-site.com:80 myCoolServer
# 9000 being the local port, some-restricted-site.com being the site you want to proxy too,
# and myCoolServer being the server that allows connections to some-restricted-site.com
# At this point http://localhost:9000 should resolve to some-restricted-site.com
# To access these from inside docker container: http://docker.for.mac.localhost:9000
@scwood
scwood / proxies.js
Last active March 6, 2018 23:01
Limiting concurrency of a module with a JavaScript Proxy
// Imagine some library that does stuff.
const library = {
makeApiCall,
};
function makeApiCall() {
return new Promise((resolve, reject) => {
console.log('starting...');
setTimeout(() => {
@scwood
scwood / PromiseQueue.js
Last active April 19, 2022 19:04
JavaScript Promise Queue
class PromiseQueue {
constructor({concurrency = 1} = {}) {
this.concurrency = concurrency;
this.queue = [];
this.pendingPromises = 0;
this.push = this.push.bind(this);
this._dequeue = this._dequeue.bind(this);
}