Skip to content

Instantly share code, notes, and snippets.

View ericpony's full-sized avatar

ericpony ericpony

  • University of Oxford
  • Oxford
View GitHub Profile
@ericpony
ericpony / Pull all gists.js
Last active August 29, 2015 14:01
A Node.js script that downloads all gists from a user, using version 3 of the GitHub API.
#! /usr/bin/node
var request = require('request'),
fs = require('fs'),
username = 'ericpony',
dir = './gists'; // save gists here
var options = {
url: 'https://api.github.com/users/' + username + '/gists',
headers: {
public class Solution {
/**
* This is a Java implementation of Daveed Vandevoorde's optimal solution
* (linear time & space) to the discrete Maximal Rectangle Problem.
* See http://www.drdobbs.com/database/the-maximal-rectangle-problem/184410529
* for problem description and details of algorithm. Also see
* https://gist.github.com/ericpony/0cb44996f43334ed28dc for a simpler variant
* of this problem.
*/
public int maximalRectangle(char[][] matrix) {
@ericpony
ericpony / Finding Dominating Members.scala
Last active August 29, 2015 14:01
Find the kth dominating members in an array in O(nk) time.
/**
* Input:
* A: An integer seq containing at least one (A,k)-dominating element
* k: An integer >= 2
* Output:
* An seq of (A,k)-dominating element(s) of A
*/
def FindDominatingNumbers(s: Seq[Int], k: Int):Seq[Int] = {
require(k>=2 && s.length>=k)
var cand = new Array[Int](k - 1)
@ericpony
ericpony / TextPrinter.scala
Last active August 29, 2015 14:01
Several ways to write text to file
def using[A <: {def close(): Unit}, B](res: A)(op: A => B): B = try op(res) finally res.close
def writeToFile(path: String, data: String): Unit =
using(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), "UTF-8")))(_.write(data))
def appendToFile(path: String, data: String): Unit =
using(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path, true), "UTF-8")))(_.write(data))
def printToFile(path: String)(op: PrintWriter => Unit): Unit =
using(new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), "UTF-8"))))(op)
trait RationalNumberTrait {
val numerator: Int
val denominator: Int
protected def gcd(a: Int, b: Int): Int =
if (a == 0) b
else if (a == 1) 1
else gcd(b % a, a)
println("Superclass constructor: " + this.numerator + "/" + this.denominator)
@ericpony
ericpony / Parallel.scala
Last active August 29, 2015 14:01
I expect the following code to yield a random permutation of {1,2,3,4}. However, it always outputs 1,2,3,4 in order. What's wrong with it?
(1 to 4).foreach(i => {
val f = scala.actors.Futures.future {
Thread.sleep(scala.util.Random.nextInt(10) * 500)
println(i)
}
f()
})
@ericpony
ericpony / Profiler.scala
Created May 8, 2014 08:22
Simple scala profiling
/*************** Method I ****************/
def time[R](block: => R): R = {
val t0 = System.nanoTime()
val result = block // call-by-name
val t1 = System.nanoTime()
println("Elapsed time: " + (t1 - t0)/1000000 + "ms")
result
}
// Now wrap your method calls with profiler
val result = time { 1 to 1000 sum }