Skip to content

Instantly share code, notes, and snippets.

@laran
laran / setup_ruby_koans_on_mac.zsh
Last active April 14, 2024 18:18
Download Ruby koans. Initialize as a git repo. Add a script to watch for changes and rerun path_to_enlightenment.rb
#!/usr/bin/env zsh
# NOTE: Has an implicit dependency on Homebrew
# NOTE: Expects Ruby and Git to already be installed.
# Define the URL and target directory
zip_file="rubykoans.zip"
url="https://github.com/edgecase/ruby_koans/blob/master/download/${zip_file}?raw=true"
# Download the zip file
@laran
laran / for-element-of-list.js
Last active October 31, 2018 16:59
Iterate elements in list
let list = [ 'val1', 'val2' ];
// Least nice
for(let i = 0; i < list.length; i++) {
console.log(list[i]);
}
// Nicer
list.forEach((element) => {
console.log(element);
@laran
laran / for-attribute-in-thing.js
Last active October 31, 2018 17:00
Iterate attributes in object
let thing = { attr1: 'val1', attr2: 'val2' };
// Least nice
let keys = Object.keys(thing);
for(let i = 0; i < keys.length; i++) {
console.log(thing[keys[i]]);
}
// Nicer
Object.keys(thing).forEach((attribute) => {
exports.handler = async function(event, context) {
// Your code goes here ...
let success = function() {
// Do stuff ...
};
// Exit the handler by invoking the callback.
// The first argument is an error object (null indicates success).
@laran
laran / Hanoi-output.txt
Created October 6, 2018 08:05
Output of Hanoi with 5 discs
Move #0: Initial state
1
2
3
4
5
- - -
Move #1: Move value=1 from Tower=0 to Tower=2
2
@laran
laran / adjacency-matrix.js
Last active September 30, 2018 00:10
An Adjacency Matrix representation of a Graph
// - An Adjacency Matrix stores the Set of all possible Edges.
// - Each row represents a Node, storing the complete Set of possible Edges for that Node.
// - The index of the element in the outer Array indicates the ID of the Node.
// - A value of 1 indicates an Edge. A 0 indicates no Edge.
//
// In this example:
// - Node 0 has an Edge to Node 3
// - Node 1 has Edges to Nodes 0 and 3
// - Node 2 has Edges to Nodes 0, 1 and 3
// - Node 3 has an Edge to Node 1
@laran
laran / adjacency-list.js
Last active October 1, 2018 03:36
A Adjacency List representation of a Graph
// The index of the element in the outer Array is the 'id' of the Node in the Graph.
// Each element in the outer Array contains the Set of IDs to which that Node is connected.
//
// e.g. Node 0 has Edges to Nodes 1 and 2
// Node 1 has no Edges
// Node 2 has Edges to Nodes 0, 1 and 3
// Node 3 only has an Edge to Node 2
let graphAsList = [
[1, 2],
@laran
laran / edge-list.js
Last active September 29, 2018 22:52
An Edge List representation of a Graph
// Each element in the Array represents an Edge between the two Nodes with the IDs in that element.
// Every element in the array will have exactly two values.
// The indices of the elements in either the inner our outer Arrays have no meaning.
let graph = [[1, 2], [0, 3], [1, 3], [2, 0]];
@laran
laran / .babelrc
Last active September 29, 2018 18:33
All permutations of an array
{
"presets": [
"env"
],
"env": {
"test": {
"presets": [
"env"
]
}
@laran
laran / binary-tree-insert.js
Last active September 28, 2018 16:04
Add a value to a Binary Tree.
import Node from 'binary-tree-node';
class BinaryTree {
constructor() {
this.root = new Node();
}
// ... other methods
add(value) {