Skip to content

Instantly share code, notes, and snippets.

@tannerwelsh
tannerwelsh / sort.js
Created July 17, 2017 19:16
Example sort implementation
/*
A very basic sorting function (not at all optimized)
Might be useful if you're trying to understand how Array#sort() works
The way this sorting "algorithm" works is as follows:
1. start with an unsorted array of items and a comparison function
2. iterate through each item in the unsorted array
3. if there are no items in the sorted array
const rp = require('request-promise')
const cheerio = require('cheerio')
const buildURL = (searchQuery) => {
return `http://www.imdb.com/find?q=${searchQuery}&s=tt&ref_=fn_al_tt_mr`
}
const parseResults = ($) => {
return $('td.result_text')
.map((i, el) => $(el).text().replace(/ - /g, "\n - "))
@tannerwelsh
tannerwelsh / random_word.sh
Created July 10, 2017 18:44
Get a random word of length n
#!/usr/bin/env sh
word_length=$1
all_words=/usr/share/dict/words
# use outer wrapping parens to convert to an array
words=( $(grep -E "^.{$word_length}$" $all_words) )
num_words=${#words[@]} # get length of words array
index=$(($RANDOM % $num_words)) # make a random index
@tannerwelsh
tannerwelsh / Gemfile
Last active July 10, 2017 17:21
Piano Tutor
source 'https://rubygems.org'
gem 'unimidi'
gem 'midi-message'
gem 'activesupport', '~> 4.2.3'
@tannerwelsh
tannerwelsh / commitment.js
Created July 7, 2017 16:44
Reverse-engineering Promise (loosely)
// This is a very bare-bones (and incomplete) implementation of the idea
// behind Promise, using a new function called Commitment:
const Commitment = function(func) {
this.state = 'PENDING' // will be changed to 'FULFILLED' or 'REJECTED'
const resolve = (result) => {
this.state = 'FULFILLED'
this.resolver && this.resolver(result)
}
@tannerwelsh
tannerwelsh / basicWords.js
Created March 30, 2017 19:37
Check if word is included in a set of 1000 basic English words
// List of 1000 basic English words from Wikipedia
// https://simple.wikipedia.org/wiki/Wikipedia:List_of_1000_basic_words
var BASIC_WORDS = ['a', 'about', 'above', 'across', 'act', 'active', 'activity', 'add', 'afraid', 'after', 'again', 'age', 'ago', 'agree', 'air', 'all', 'alone', 'along', 'already', 'always', 'am', 'amount', 'an', 'and', 'angry', 'another', 'answer', 'any', 'anyone', 'anything', 'anytime', 'appear', 'apple', 'are', 'area', 'arm', 'army', 'around', 'arrive', 'art', 'as', 'ask', 'at', 'attack', 'aunt', 'autumn', 'away', 'baby', 'back', 'bad', 'bag', 'ball', 'bank', 'base', 'basket', 'bath', 'be', 'bean', 'bear', 'beautiful', 'bed', 'bedroom', 'beer', 'behave', 'before', 'begin', 'behind', 'bell', 'below', 'besides', 'best', 'better', 'between', 'big', 'bird', 'birth', 'birthday', 'bit', 'bite', 'black', 'bleed', 'block', 'blood', 'blow', 'blue', 'board', 'boat', 'body', 'boil', 'bone', 'book', 'border', 'born', 'borrow', 'both', 'bottle', 'bottom', 'bowl', 'box', 'boy', 'branch', 'brave', '
require_relative "university"
University.open_db
student = University::Student.new('Tanner Welsh')
student.save
student = University::Student.new('Mike Busch')
student.save
@tannerwelsh
tannerwelsh / gistclone
Last active December 16, 2015 15:19
Easily clone gists that you own using SSH instead of HTTP
#!/bin/bash
# <<< gistclone >>>
#
# Why clone gists with HTTP when you can use SSH?
#
# Just put this somewhere in your PATH, make it executable, and you're good to go.
#
# Usage:
# $ gistclone <gist_url> [<directory>]
#!/bin/bash
echo "Installing debugger gem..."
gem install 'debugger'
echo "Creating and modifying ~/.rdebugrc config file..."
touch ~/.rdebugrc
echo -e "set autolist\nset autoeval\nset autoreload" > ~/.rdebugrc
@tannerwelsh
tannerwelsh / find_missing_number.rb
Created September 19, 2012 05:45
Find Missing Number
# Setup an array of 99 numbers between 1 and 100, randomly sorted
@numbers = (1..1_000_000).to_a.shuffle
@numbers.delete_at(rand(1_000_000))
def sort_find
sorted = @numbers.sort
sorted.each_with_index do |n, i|
next if n == sorted[i-1] + 1 || n == 1
return n-1