Skip to content

Instantly share code, notes, and snippets.

package main
import (
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
@irpap
irpap / Mnemonics.scala
Last active December 20, 2015 14:39
Code from the Functional Programming in Scala session.
import org.scalatest.FunSuite
class MnemonicsDemoSolution extends FunSuite {
class Coder(val words: List[String]) {
val mnem = Map(
'2' -> "ABC", '3' -> "DEF", '4' -> "GHI", '5' -> "JKL",
'6' -> "MNO", '7' -> "PQRS", '8' -> "TUV", '9' -> "WXYZ")
/** Invert the mnem map to give a map from chars 'A'...'Z' to '2'...'9' */
val charCode: Map[Char, Char] =
@irpap
irpap / gist:3896215
Created October 15, 2012 23:02
One dimensional game of life
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.FunSuite
@RunWith(classOf[JUnitRunner])
class GameOfLifeTest extends FunSuite {
type Rules = ((Int, Int, Int)) => Int
case class TheRules(rules: List[Int]) extends Rules {
@irpap
irpap / maxBipartiteMatching.java
Created August 25, 2012 11:24
Maximum Bipartite Matching using DFS
int m, n;
boolean[][] graph;
boolean seen[];
int matchL[]; //What left vertex i is matched to (or -1 if unmatched)
int matchR[]; //What right vertex j is matched to (or -1 if unmatched)
int maximumMatching() {
//Read input and populate graph[][]
//Set m to be the size of L, n to be the size of R
Arrays.fill(matchL, -1);
@irpap
irpap / FordFulkerson.java
Created August 24, 2012 19:51
Ford Fulkerson
int fordFulkerson(int n, int s, int t) {
//ASSUMES: cap[u][v] stores capacity of edge (u,v). cap[u][v] = 0 for no edge.
//Initialise the flow network so that fnet[u][v] = 0 for all u,v
int flow = 0; //no flow yet
while (true) {
//Find an augmenting path using BFS
int[] prev = new int[n];
Arrays.fill(prev, -1);
LinkedList<Integer> queue = new LinkedList<Integer>();