Skip to content

Instantly share code, notes, and snippets.

@jtribble
jtribble / graphs.py
Created August 1, 2020 19:57
A simple graph represention in Python along with some common methods, like breadth-first traversal, depth-first traversal, and shortest path.
#!/usr/bin/env python3
from collections import deque
from dataclasses import dataclass, field
from typing import Set, Optional, List, Generator, Dict, Deque
from unittest import TestCase, main
@dataclass
class GraphNode:
label: str
@jtribble
jtribble / binary_trees.py
Last active August 1, 2020 02:04
Python implementations of some common binary tree operations, such as breadth-first traversal (BFS) and depth-first traversals (DFS; pre-order, in-order, post-order, both recursive and iterative).
#!/usr/bin/env python3
from collections import deque, defaultdict
from dataclasses import dataclass
from typing import Generator, Optional, Deque, List, Tuple, DefaultDict
from unittest import TestCase, main
@dataclass
class BinaryTree:
value: int
@jtribble
jtribble / install_python_coreos.sh
Last active April 26, 2019 22:16
Install Python on CoreOS (e.g., to bootstrap for Ansible)
#!/usr/bin/env bash
set -euo pipefail # See: http://redsymbol.net/articles/unofficial-bash-strict-mode/
IFS=$'\n\t'
if [[ -d ~/.portable-pypy ]]; then
echo "Directory already exists: ~/.portable-pypy"
exit 0
fi
echo "Installing portable pypy for user '${USER}' at location '${HOME}/.portable-pypy'"
@jtribble
jtribble / count-comparisons.js
Last active October 29, 2016 20:18
Count the number of comparisons required by quicksort to sort a list of numbers using three distinct pivot selection strategies: choose first, choose last, and choose median of three.
#!/usr/bin/env node
//
// Count the number of comparisons required by quicksort using the following
// three pivot-selection strategies:
//
// 1. Always pick the first element.
// 2. Always pick the last element.
// 3. Pick the median of the first, last, and middle elements. If the list
// length is even, pick the element with the smaller index.
//
@jtribble
jtribble / count-inversions.js
Created October 9, 2016 22:09
Count the number of inversions in a list of integers using merge sort.
#!/usr/bin/env node
//
// The point of this program is to count the number of inversions in a list of
// integers using merge sort. The number of inversions can be used to calculate
// similarity between two ranked lists.
//
// For example, consider a case where two people are asked to rank a list of 10
// movies by some qualitative measure. We can use the sorted list from
// [1,...,10] to represent the first person's rankings, where the numbers 1-10
// represent the movies they've chosen to inhabit those ranks. The second
//
// Component with ES2015 features
//
import React, {PropTypes} from 'react';
const List = React.createClass({
propTypes: {
items: PropTypes.array.isRequired,
onItemClick: PropTypes.func.isRequired
@jtribble
jtribble / frequent-words.js
Created April 30, 2016 00:05
Given a random array of words, find all duplicates of words and return the 1st "n" in order from most frequent to least frequent (words with identical frequency would be listed alphabetically)
// Given a random array of words, find all duplicates of words and return the
// 1st "n" in order from most frequent to least frequent (words with identical
// frequency would be listed alphabetically)
/**
* Find the most frequent words in an array of words
*
* @param {array} words - The list of words
* @param {number} n - The number of results to show
* @return {array} - An array of word+count objects, sorted
@jtribble
jtribble / permutations.js
Last active March 19, 2016 21:59
Find all permutations of a string using base case & build approach. See: https://en.wikipedia.org/wiki/Permutation
//
// Find all permutations of a string using base case & build approach
//
// See: https://en.wikipedia.org/wiki/Permutation
//
'use strict';
if (!String.prototype.splice) {
/**
* @method - Add "splice" to String
@jtribble
jtribble / haversine-distance.js
Created March 7, 2016 19:14
Compute the distance between two points using the Haversine formula. See: https://en.wikipedia.org/wiki/Haversine_formula
//
// Compute the distance between two points using the Haversine formula
//
// See: https://en.wikipedia.org/wiki/Haversine_formula
//
// Example: geoCoordDistance(
// {lat: '48° 12′ 30″ N', lng: '16° 22′ 23″ E'},
// {lat: '23° 33′ 0″ S', lng: '46° 38′ 0″ W'}
// ) => 10130
//
@jtribble
jtribble / material-design-color-palette.scss
Last active August 10, 2019 15:53
Google Material Design color palette variables for PostCSS/Sass. See: http://www.google.com/design/spec/style/color.html
/**
* Google Color Palette: http://www.google.com/design/spec/style/color.html
*/
$materialize-red-lighten-5: #fdeaeb;
$materialize-red-lighten-4: #f8c1c3;
$materialize-red-lighten-3: #f3989b;
$materialize-red-lighten-2: #ee6e73;
$materialize-red-lighten-1: #ea454b;
$materialize-red-base: #e51c23;