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 / 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 ")
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 / 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

/**
* A disjoint sets data structure.
*
* @author Michael E. Cotterell <[email protected]>
* @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 / 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
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 / 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) }
@mepcotterell
mepcotterell / Schema.scala
Created September 7, 2012 12:46
TypeTags Example for Database Schema
// NOT TESTED YET
import scala.reflect.runtime.universe._
class Schema[T : TypeTag](val attributes: String*) extends Dynamic {
val length = attributes.length
def selectDynamic[A: TypeTag](field: String): A.tpe = null
def updateDynamic[A: TypeTag](field: String)(value: A.tpe) { }
} // Schema
@mepcotterell
mepcotterell / actor.rdf
Created April 2, 2012 15:21
Example RDF output of Sakila database.
<http://johnson.cs.uga.edu/triplify/index.php/actor> <http://www.w3.org/2000/01/rdf-schema#comment> "Generated by Triplify V0.7.1 (http://Triplify.org)" .
<http://johnson.cs.uga.edu/triplify/index.php/actor> <http://creativecommons.org/ns#license> <http://creativecommons.org/licenses/by/3.0/us/> .
<http://johnson.cs.uga.edu/triplify/index.php/actor/1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Actor> .
<http://johnson.cs.uga.edu/triplify/index.php/actor/2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Actor> .
<http://johnson.cs.uga.edu/triplify/index.php/actor/3> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Actor> .
<http://johnson.cs.uga.edu/triplify/index.php/actor/4> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Actor> .
<http://johnson.cs.uga.edu/triplify/index.php/actor/5> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Actor> .
<http://jo
google.load('visualization', '1', {'packages': ['geochart']});
google.setOnLoadCallback(drawMarkersMap);
function drawMarkersMap() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'City');
data.addColumn('number', 'Population');
data.addColumn('number', 'Area');
data.addRows([
['Rome', 2761477, 1285.31],