Skip to content

Instantly share code, notes, and snippets.

@methodin
methodin / viterbi.js
Created January 7, 2012 21:59
Viterbi
// Viterbi algorithm for finding hidden relationships
function Viterbi(data) {
var V = [{}];
var path = {};
// Initialize base cases (t == 0)
for(var i=0;i<data.states.length;i++) {
var state = data.states[i];
V[0][state] = data.start_probability[state] * data.emission_probability[state][data.observations[0]];
path[state] = [state];
@methodin
methodin / Rotate3dAnimation.java
Last active April 28, 2021 07:25
3d rotation animation for Android
/*
Original code found at:
https://code.google.com/p/android-stocker/source/browse/trunk/src/com/twofuse/stocker/Rotate3dAnimation.java?r=2
Use (in set):
Rotate3dAnimation skew = new Rotate3dAnimation(20, 0, 0, 0, 0, 0);
set.addAnimation(skew);
animation = new TranslateAnimation(0, 0, 0, 0, Animation.RELATIVE_TO_SELF, 0.5f, 0, 0);
set.addAnimation(animation);
@methodin
methodin / prims.js
Created January 8, 2012 06:33
Prim's Algorithm
// Represents an edge from source to sink with capacity
var Edge = function(source, sink, capacity) {
this.source = source;
this.sink = sink;
this.capacity = capacity;
};
// Main class to manage the network
var Graph = function() {
this.edges = {};
@methodin
methodin / jquery.background-noise.js
Created March 4, 2011 02:42
jQuery background-noise
jQuery.fn.noisy = function(opacity) {
if (typeof(opacity) === 'undefined') {
opacity = 0.1;
}
var wrapper = jQuery(this).wrapInner('<div />').children();
var canvas = document.createElement("canvas");
canvas.width = 100;
canvas.height = 100;
var ctx = canvas.getContext("2d");
@methodin
methodin / fordFulkerson.js
Created January 4, 2012 20:08
Ford-Fulkerson Max Flow
// Represents an edge from source to sink with capacity
var Edge = function(source, sink, capacity) {
this.source = source;
this.sink = sink;
this.capacity = capacity;
this.reverseEdge = null;
this.flow = 0;
};
// Main class to manage the network
@methodin
methodin / DB.java
Created October 3, 2011 01:36
General database handler for Android
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/drawer_content_padding"/>
@methodin
methodin / TimerExample.java
Created June 11, 2013 18:14
Setting up local timers in fragments/activities
public class DummyFragment extends Fragment {
private UpdateHandler handler;
private Timer timer;
@Override
public void onCreate(Bundle savedInstanceState) {
handler = new UpdateHandler(this);
super.onCreate(savedInstanceState);
}
@methodin
methodin / ListLoader.java
Created May 30, 2013 15:36
Using an AsyncLoader with ActiveAndroid
/*
* Example:
* new ListLoader(listView).execute(new Select().from(TestEntity.class));
*/
import java.lang.ref.WeakReference;
import java.util.List;
import android.os.AsyncTask;
import android.widget.ListView;
@methodin
methodin / compojure-enlive.clj
Last active December 14, 2015 19:49
Compojure example with Enlive template inheritance
(ns list.handler
(:use compojure.core
ring.adapter.jetty
[net.cgrand.enlive-html :as html])
(:require [compojure.handler :as handler]
[compojure.route :as route])
(:gen-class))
(defmacro maybe-substitute
([expr] `(if-let [x# ~expr] (html/substitute x#) identity))