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 / MontyPython.java
Created January 8, 2014 02:10
Uses reflection to count like King Arthur with the Holy Hand Grenade
import java.lang.reflect.Field;
public class MontyPython {
public static void main(String[] args) throws Exception {
Field v = Integer.class.getDeclaredField("value");
v.setAccessible(true);
v.set(3,5);
System.out.printf("Ready the holy hand grenade!\n");
Thread.sleep(1000);
System.out.printf("%d\n", 1);
Thread.sleep(1000);
@rtoal
rtoal / Mystery.java
Created March 30, 2014 17:11
Something I found and cleaned up a little bit
import java.util.Random;
public class Mystery {
public static void main(String[] args) {
System.out.println(randomString(-229985452) + " " + randomString(-147909649));
}
private static String randomString(int code) {
Random random = new Random(code);
@rtoal
rtoal / bytestring.js
Created June 21, 2014 21:08
8-bit bytestring hack in JavaScript
function byteString(n) {
if (n < 0 || n > 255 || n % 1 !== 0) {
throw new Error(n + " does not fit in a byte");
}
return ("000000000" + n.toString(2)).substr(-8)
}
@rtoal
rtoal / read_zipped_json_from_s3.py
Created October 1, 2014 15:16
Read gzipped JSON file from S3
import json
import gzip
import cStringIO
from boto.s3.connection import S3Connection
from boto.s3.key import Key
AWSACCESSKEY='********************'
AWSSECRETKEY='****************************************'
def read_gzipped_json_file_from_s3(bucket_name, key_name):
@rtoal
rtoal / animals.coffee
Created October 27, 2014 03:46
An example of inheritance and polymorphism in CoffeeScript
# A typical example of inheritance
class Animal
constructor: (@name) ->
speak: ->
"#{@name} says #{this.sound()}"
class Cow extends Animal
sound: -> "moo"
@rtoal
rtoal / animals.py
Created October 27, 2014 03:47
An example of inheritance and polymorphism in Python
"""A module with talking animals."""
class Animal(object):
def __init__(self, name):
self.name = name
def speak(self):
print self.name, 'says', self.sound()
class Cow(Animal):
@rtoal
rtoal / animals.scala
Created October 27, 2014 03:52
An example of inheritance and polymorphism in Scala
abstract class Animal(name: String) {
def speak = name + " says " + sound
def sound: String
}
class Cow(name: String) extends Animal(name) {
override def sound() = "moooooo"
}
class Horse(name: String) extends Animal(name) {
@rtoal
rtoal / AnimalDemo.java
Created October 27, 2014 04:22
An example of inheritance and polymorphism in Java
abstract class Animal {
private String name;
public Animal(String name) {
this.name = name;
}
public String speak() {
return name + " says " + sound();
}
public abstract String sound();
}
@rtoal
rtoal / freshman-practice.js
Last active August 29, 2015 14:10
A little node.js script containing answers to practice problems for a freshman programming class I teach
// These are answers to practice problems for the freshmen.
//
// Note they mignt not be the fanciest possible solutions because they are
// problems for a beginner class.
var sameLength = function (a, b) {
return a.length === b.length;
}
var evenlyDivides = function (x, y) {
@rtoal
rtoal / homework-0-calc.js
Created January 14, 2015 08:06
A warmup exercise for a class
C.evalAST = function (ast) {
var environment = {}
function ev(ast) {
return !Array.isArray(ast) ? ast : ops[ast[0]].apply(undefined, ast.slice(1))
}
var ops = {
id : function(x) {return environment.hasOwnProperty(x) ? environment[x] : 0},
set : function(id, exp) {return environment[id] = ev(exp)},