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 / 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 }
@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()
})
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 / 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)
@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)
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 / 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: {
@ericpony
ericpony / Maximal Rectangle in Histogram.java
Last active August 29, 2015 14:01
Optimal solution to finding the area of the largest rectangle in histogram. (http://oj.leetcode.com/problems/largest-rectangle-in-histogram)
public class Solution {
/**
* This is a O(n) algorithm for finding the area of the largest rectangle
* in histogramand and is a simple version of Daveed V's optimal algorithm
* for finding the largest rectangle in a 2D matrix.
* See https://gist.github.com/ericpony/15c1a126980f36652e6a for details.
*/
public int largestRectangleArea(int[] height) {
if(height==null || height.length==0)
@ericpony
ericpony / Linked List Cycle II.java
Last active August 29, 2015 14:01
Given a linked list, return the node where a cycle begins, or return null if there is no cycle.
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
function interpret(form, env, ctx){
if(form instanceof Array){
switch(form[0]){
case 'lambda': {
var params = form[1];
var body = form[2];
return ctx(function(ctx){ return function() {
var e = Object.create(env);
for(var j = 0; j < params.length; j++)
e[params[j]] = arguments[j];