Skip to content

Instantly share code, notes, and snippets.

View rtoal's full-sized avatar
💭
Copying and pasting

Ray Toal rtoal

💭
Copying and pasting
View GitHub Profile
@rtoal
rtoal / mongo-sharding-transcript.js
Created June 6, 2013 18:11
Transcript for the sharding example from a mongo workshop
mkdir -p data/shard0/rs0 data/shard0/rs1 data/shard0/rs2
mongod --replSet s0 --logpath "s0-r0.log" --dbpath data/shard0/rs0 --port 37017 --fork --shardsvr
mongod --replSet s0 --logpath "s0-r1.log" --dbpath data/shard0/rs1 --port 37018 --fork --shardsvr
mongod --replSet s0 --logpath "s0-r2.log" --dbpath data/shard0/rs2 --port 37019 --fork --shardsvr
mongo --port 37017
config = {_id: "s0", members: [
{_id: 0, host: "localhost:37017" },
{_id: 1, host: "localhost:37018" },
{_id: 2, host: "localhost:37019" }]};
@rtoal
rtoal / core-class-hiearchy.rb
Created July 17, 2013 03:06
Generates the tree of Ruby core classes that aren't themselves in any modules
# Writes the class hierarchy of the core Ruby classes.
$nodes = Hash.new
module Hack # Hack to keep Node out of the result!
class Node
attr_reader :cls
attr_accessor :children
def initialize(c)
@cls = c
@rtoal
rtoal / ruby-2.0.0p247-core-classes.txt
Created July 17, 2013 03:12
Output of gist 6017359 using Ruby 2.0.0p247
BasicObject
Object (Kernel)
Module
Class
NilClass
Data
TrueClass
FalseClass
Encoding
String (Comparable)
@rtoal
rtoal / circle.rb
Last active December 19, 2015 21:09
A trivial Ruby circle class
class Circle
attr_reader :r
def initialize(x = 0, y = 0, r = 1)
@x = x
@y = y
@r = r
end
def center
@rtoal
rtoal / animals.rb
Created July 17, 2013 07:32
Demonstration of Inheritance and Polymorphism in Ruby
class Animal
def initialize(name)
@name = name
end
def speak()
"#{@name} says #{sound()}"
end
end
class Cow < Animal
@rtoal
rtoal / find_json_properties.py
Created July 31, 2013 20:34
Guess the properties of a JSON file, read from stdin. Not perfect.
import re
import sys
pattern = re.compile(r'"[\w]+":')
all = set([])
for line in sys.stdin:
keys = re.findall(pattern, line)
all.update(keys)
print all
@rtoal
rtoal / bypass.c
Created November 4, 2013 04:03
A fun executable to try on a 32-BIT x86 machine. I wrote this years and years and years ago.
/*
* bypass.c
*
* Try this on an x86 machine. It does **not** print "1".
*/
#include <stdio.h>
void weird() {
int a;
@rtoal
rtoal / callby.py
Created December 28, 2015 05:16
Determines whether Python is call-by-value, call-by-reference, or call-by-sharing
arg = {'x': 0}
def f(param):
same_ids = id(arg) == id(param)
different_ids = not same_ids
param = {'y': 4}
arg_changed = 'y' in arg
print('Call by value: {}'.format(different_ids))
print('Call by sharing: {}'.format(same_ids and not arg_changed))
print('Call by reference: {}'.format(same_ids and arg_changed))
@rtoal
rtoal / timecomplexity.py
Last active January 31, 2016 19:17
Run times for various complexity functions, just for fun
# Runtimes for various complexity functions, sort of.
#
# A hacked-together little Python3 script printing runtimes for various
# complexity functions to standard output.
#
# The script isn't very general or customizable; you have to edit the
# source code to explore different functions or use different values
# of n.
import math
@rtoal
rtoal / water_10_7_4.py
Created April 26, 2016 03:28
The famous 10-7-4 water jug problem
# 10-7-4 Water Jug Problem
# Get 2 units of water in the 7-container or the 4-container
# Starting state is (0,7,4)
# Operations are F10, F7, F4, T107, T104, T710, T74, T410, T47
# Goal states are (x,2,z) and (x,y,2)
operations = {
'F10' : lambda x, y, z: [10, y, z],