Skip to content

Instantly share code, notes, and snippets.

View hans's full-sized avatar

Jon Gauthier hans

View GitHub Profile
@hans
hans / model1_em.py
Created February 18, 2014 22:46
Implementation of the IBM model 1 expectation-maximization algorithm for learning word alignments.
#!/usr/bin/env python
"""An implementation of the IBM Model 1 expectation-maximization
algorithm for learning word alignments.
"""
from collections import defaultdict
import copy
import itertools
import operator
@hans
hans / courses.md
Last active December 21, 2015 17:08
Stanford 2013 courses grouped by GER / WAY requirements
@hans
hans / 01.install.sh
Created October 21, 2012 19:33
smlnj failed to build on 10.8.2
config/install.sh: Using shell /bin/sh.
config/install.sh: SML root is /usr/local/Cellar/smlnj/110.74/libexec.
config/install.sh: Installation directory is /usr/local/Cellar/smlnj/110.74/libexec.
config/install.sh: Installing version 110.74.
config/install.sh: URL of source archive is http://smlnj.cs.uchicago.edu/dist/working/110.74/.
config/install.sh: Script /usr/local/Cellar/smlnj/110.74/libexec/bin/.arch-n-opsys reports ARCH=x86; OPSYS=darwin; HEAP_SUFFIX=x86-darwin.
/usr/local/Cellar/smlnj/110.74/libexec/config/unpack: Fetching run-time from http://smlnj.cs.uchicago.edu/dist/working/110.74/. Please stand by...
/usr/local/Cellar/smlnj/110.74/libexec/config/unpack: Trying runtime.tgz ...
/usr/local/Cellar/smlnj/110.74/libexec/config/unpack: Fetching runtime.tgz was a success.
/usr/local/Cellar/smlnj/110.74/libexec/config/unpack: Un-GZIP-ing and un-TAR-ing run-time archive.
@hans
hans / binary-tree.clj
Created March 4, 2012 16:43
Non-performant binary tree implementation in Clojure
(defrecord BinaryTree [key left right]))
(defn insert [tree val]
(if (nil? tree)
(BinaryTree. val nil nil)
(let [key (:key tree)]
(cond
(< key val) (BinaryTree. key
(:left tree)
(insert (:right tree) val))
(ns euler.utils.derivative
(:use clojure.contrib.math))
(defn take-with-epsilon
"Take from a list of numbers xs until two numbers are within the epsilon eps.
Check a maximum of `limit` times. Returns the last number of the satisfying
pair or nil of no match is found."
[xs eps limit]
(let [next (rest xs)]
@hans
hans / leibniz.java
Created August 28, 2011 04:58
Approximating pi with the Leibniz formula
class Leibniz {
public static void main(String[] args) {
Leibniz test = new Leibniz();
System.out.println(test.leibnizPi(0));
System.out.println(test.leibnizPi(-1));
System.out.println(test.leibnizPi(-4));
}
public double leibnizPi(int accuracy) {
double target_accuracy = Math.pow(10, accuracy);
@hans
hans / leibniz.clj
Created August 28, 2011 04:57
Approximating pi with the Leibniz formula
(use 'clojure.contrib.math)
(defn leibniz-pi [accuracy]
(let [target-accuracy (expt 10 accuracy)]
(loop [acc 1.0 sub 3 sign -1]
(let [attempt (* 4.0 acc)]
(if (> (abs (- attempt Math/PI)) target-accuracy)
(recur (+ acc (* sign (/ 1.0 sub))) (+ 2 sub) (- sign))
attempt)))))
@hans
hans / JavaMail_SMTP_with_authentication.java
Created July 29, 2011 23:00
JavaMail: Sending an email message to an SMTP server which uses authentication
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
@hans
hans / gist:950781
Created May 1, 2011 19:31
a function which will split a sequence into two parts
;; (split 3 [1 2 3 4 5 6]) => [[1 2 3] [4 5 6]]
;; (split 1 [:a :b :c :d]) => [[:a] [:b :c :d]]
(defn split [n xs]
(loop [acc '(), inacc '(), flxs (first xs), lxs (rest xs), cnt n]
(print acc inacc cnt "\n")
(if (nil? flxs)
(reverse (conj acc (reverse inacc)))
(if (zero? cnt)
(recur (conj acc (reverse inacc)) nil flxs lxs -1)
@hans
hans / gist:950757
Created May 1, 2011 19:12
a higher-order function which flips the order of the arguments of an input function
(fn [thefn]
(fn [& args]
(apply thefn (reverse args))))