Skip to content

Instantly share code, notes, and snippets.

View echojc's full-sized avatar

Jonathan Chow echojc

View GitHub Profile
@echojc
echojc / pbday
Last active December 15, 2015 05:58
Simple empirical test of birthday problem probabilities.
def bday = new Random().nextInt(365)
def uniq(n: Int) = (1 to n).map(_ => bday).distinct.size == n
def puniq(n: Int, c: Int = 1000000) = (1 to c).map(_ => uniq(n)).count(_ == true).toDouble / c
@echojc
echojc / git aliases
Created March 26, 2013 03:16
I don't think this is how they intended you to use it.
# ~/.bashrc
alias g='git'
# ~/.gitconfig
[alias]
l = log
o = checkout
p = pull --rebase
s = status
c = commit
@echojc
echojc / cdiff.js
Last active December 15, 2015 12:19
Bookmarklet to add simple diff highlighting to emails sent to Gmail by Git's default post-receive hook.
javascript:(function(){var a=document.getElementsByClassName("gt ii adP adO");for(b in a){var e=a[b];e.innerHTML="<pre style=\"font-family:Consolas;font-size:9pt\">"+e.innerHTML.replace(/^\s*(\+.*)$/gm,"<span style=\"color:green\">$1</span>").replace(/^\s*(-.*)$/gm,"<span style=\"color:red\">$1</span>").replace(/^\s*(@@.*)$/gm,"<span style=\"color:blue\">$1</span>").replace(/<br>/g,"")+"</pre>";}}());
@echojc
echojc / roman_numerals_test.rb
Last active December 17, 2015 05:59
How Corey Haines structed his tests for the Roman Numerals kata according to http://youtu.be/vX-Yym7166Y
describe "Converting arabic numbers to roman numerals" do
{
1 => "I",
2 => "II",
5 => "V"
#...
}.each_pair do |arabic, roman|
it "converts #{arabic} to #{roman}" do
expect(convert(arabic)).to eq(roman)
end
@echojc
echojc / conway_liveneighbour_test_repeating.scala
Created May 12, 2013 04:32
Repetitive way to test different inputs for a function that determines whether a cell in Conway's Game of Life should become alive or stay dead given the number of live neighbours.
describe ("alive cells") {
val cell = Cell(Alive)
it ("should become Dead when there are 0 live neighbours") {
cell.next(0) should be (Cell(Dead))
}
it ("should become Dead when there are 1 live neighbours") {
cell.next(1) should be (Cell(Dead))
}
it ("should become Alive when there are 2 live neighbours") {
cell.next(2) should be (Cell(Alive))
@echojc
echojc / conway_liveneighbour_test.scala
Created May 12, 2013 04:34
A concise way to test different inputs for a function that determines whether a cell in Conway's Game of Life should become alive or stay dead given the number of live neighbours.
describe ("alive cells") {
val cell = Cell(Alive)
Seq(
(0, Dead),
(1, Dead),
(2, Alive),
(3, Alive)
// ...
) foreach { case (count, state) =>
@echojc
echojc / listen.py
Created May 23, 2013 03:58
Quick 'n' dirty Python script to listen on a port and do nothing with the connection, simulating a server that allows you to connect but does not reply.
#!/usr/bin/python
import socket
import sys
if (len(sys.argv) != 2 or not sys.argv[1].isdigit()):
print 'Usage: listen <port>',
exit()
p = int(sys.argv[1])
l = []
transpose :: [[a]] -> [a] -> [a]
transpose [] output = output
transpose input output = transpose
(filter (\x -> not (null x)) (map (\x -> tail x) input))
(output ++ (map (\x -> head x) input))
print (transpose [[1, 2, 3], [4, 5], [6, 7, 8]] [])
-- prints [1, 4, 6, 2, 5, 7, 3, 8]
def transpose[T](input: List[List[T]]) = {
def iter[T](input: List[List[T]], output: List[T]): List[T] =
if (input.isEmpty) output
else iter(
input map { _.tail } filter { !_.isEmpty },
output ::: (input map { _.head })
)
iter(input, Nil)
}
transpose :: [[a]] -> [a]
transpose [] = []
transpose input = (map (\x -> head x) input) ++
transpose (filter (\x -> not (null x)) (map (\x -> tail x) input))