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 / 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 / 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 / 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)},
@rtoal
rtoal / permutations.lua
Created February 22, 2015 20:08
Heap's permutation algorithm as a Lua 5.3 commandline script
if #arg ~= 1 then
io.stderr:write('Exactly one argument required\n')
os.exit(1)
end
function generatePermutations(n, a)
if n == 0 then
print(utf8.char(table.unpack(a)))
else
for i = 1, n do
@rtoal
rtoal / powerpowermod.jl
Created March 8, 2015 21:27
Modular double exponentiation
function powerpowermod(a, b, c, p)
if !isprime(p)
error("Go away. I not gonna even try this.")
end
return powermod(a, powermod(b, c, p-1), p)
end
@rtoal
rtoal / vector.lua
Created March 14, 2015 19:31
Example of an interesting class pattern in Lua, with operator overloading
Vector = (function (class, meta, prototype)
class.new = function (i, j)
return setmetatable({i = i, j = j}, meta)
end
prototype.magnitude = function (self)
return math.sqrt(self.i * self.i + self.j * self.j)
end
meta.__index = prototype
meta.__add = function (self, v)
return class.new(self.i + v.i, self.j + v.j)
@rtoal
rtoal / mongo-intermediate
Last active December 17, 2015 03:28
Transcript for an intermediate-level mongo demonstration
# Mongo not happy unless lots of open files
ulimit -n 2000
# Start server
sudo mongod --fork --syslog
# Already done
unzip all-films.zip
mongoimport -d mgo -c films all-films.json