Skip to content

Instantly share code, notes, and snippets.

View mepcotterell's full-sized avatar
💭
I may be slow to respond.

Michael Cotterell mepcotterell

💭
I may be slow to respond.
View GitHub Profile
@mepcotterell
mepcotterell / Example.scala
Last active October 11, 2015 03:47
Shuffled (Uniform Random) Range in Scala
import scala.util.Random
// print unshuffled range
(1 to 10) foreach { i => println(i) }
// print shuffled range
val r = new Random()
r.shuffle(1 to 10 toIterable) foreach { i => println(i) }
import scalation.linalgebra.VectorD
import scalation.random.Normal
object NaiveBayes extends App {
val miles = VectorD(1300, 900, 2000, 1450, 2190, 1600, 300, 3100, 1521, 1786)
val mean = milel.sum / miles.dim.toDouble
val variance = ((miles - mean) ~^ 2).sum / (miles.dim.toDouble - 1)
def PrMiles (x: Double) = Normal(mean, variance).pf(x)
@mepcotterell
mepcotterell / Search.java
Created October 28, 2012 23:56
Generic Binary Search in Java
public class Search {
public static <T extends Comparable<T>> int binarySearch(T[] array, T value, int lo, int hi) {
if (lo < hi) {
int mid = (lo / 2) + (hi / 2);
int cmp = array[mid].compareTo(value);
if (cmp < 0) return binarySearch(array, value, lo, mid - 1);
if (cmp > 0) return binarySearch(array, value, mid + 1, hi);
return mid;
} // if
/**
* A disjoint sets data structure.
*
* @author Michael E. Cotterell <mepcotterell@gmail.com>
* @param n the number of initial disjoint sets
*/
class OldDisjointSets (n: Int) {
// make sure we're using mutable sets
import scala.collection.mutable.{ BitSet, Set }
@mepcotterell
mepcotterell / README.md
Created November 13, 2012 10:44
QuickSort

QuickSort

QuickSort is a sorting algorithm developed by Tony Hoare that, on average, makes O(n log n) comparisons to sort n items. It is also known as partition-exchange sort because of its use of the partition algorithm. In the worst case, it makes O(n2) comparisons, though this behavior is rare. QuickSort is often faster in practice than other O(n log n) algorithms.

Partition Algorithm

public class Armstrong {
public static void main(String[] args) {
for (int a = 0; a < 9; a++) {
for (int b = 0; b < 9; b++) {
for (int c = 0; c < 9; c++) {
int n = a * 100 + b * 10 + c;
int a3 = a * a * a;
int b3 = b * b * b;
int c3 = c * c * c;
if (a3 + b3 + c3 == n) System.out.println(n);
@mepcotterell
mepcotterell / gist:4548901
Created January 16, 2013 17:14
Add line numbers to Emacs
;; add line numbers
(global-linum-mode 1)
;; display line numbers and column numbers
(setq line-number-mode t)
(setq column-number-mode t)
;; make sure the line numbers don't touch the text
(setq linum-format "%d ")
@mepcotterell
mepcotterell / gist:4567532
Last active December 11, 2015 07:38
Intermediate Programming with Java

Pass by Value in Java

Variable Declaration and Memory

When you declare a variable of any type (including class/reference types), some space is reserved in memory in order to hold the value of the variable. The variable is just a symbolic name for this value in memory.

Let's say we declare a long.

long x;
@mepcotterell
mepcotterell / LCG.scala
Created January 27, 2013 00:12
Stream-based Linear Congruential Generator in Scala
/**
* Linear Congruential Generator
*
* This implementation allows you to grab an individual number by its index. It
* also provides a stats object for displaying statistics about the lcg.
*
* @see http://en.wikipedia.org/wiki/Linear_congruential_generator
* @author Michael E. Cotterell <mepcotterell@gmail.com>
* @param a the multiplier
* @param c the increment
@mepcotterell
mepcotterell / Examples.md
Last active December 11, 2015 23:08
Examples from class...

Git Examples

Configuration

The first thing you'll want to do is configure git on your nike account.

$ git config --global user.name "Your Name Here"
$ git config --global user.email "your_email@youremail.com"
$ git config --global --add color.ui true