Skip to content

Instantly share code, notes, and snippets.

# Graham Scan - Tom Switzer <thomas.switzer@gmail.com>
TURN_LEFT, TURN_RIGHT, TURN_NONE = (1, -1, 0)
def turn(p, q, r):
return cmp((q[0] - p[0])*(r[1] - p[1]) - (r[0] - p[0])*(q[1] - p[1]), 0)
def _keep_left(hull, r):
while len(hull) > 1 and turn(hull[-2], hull[-1], r) != TURN_LEFT:
hull.pop()
# Jarvis March O(nh) - Tom Switzer <thomas.switzer@gmail.com>
TURN_LEFT, TURN_RIGHT, TURN_NONE = (1, -1, 0)
def turn(p, q, r):
"""Returns -1, 0, 1 if p,q,r forms a right, straight, or left turn."""
return cmp((q[0] - p[0])*(r[1] - p[1]) - (r[0] - p[0])*(q[1] - p[1]), 0)
def _dist(p, q):
"""Returns the squared Euclidean distance between p and q."""
# Chan's Convex Hull O(n log h) - Tom Switzer <thomas.switzer@gmail.com>
TURN_LEFT, TURN_RIGHT, TURN_NONE = (1, -1, 0)
def turn(p, q, r):
"""Returns -1, 0, 1 if p,q,r forms a right, straight, or left turn."""
return cmp((q[0] - p[0])*(r[1] - p[1]) - (r[0] - p[0])*(q[1] - p[1]), 0)
def _keep_left(hull, r):
while len(hull) > 1 and turn(hull[-2], hull[-1], r) != TURN_LEFT:
@tixxit
tixxit / bsort.js
Created February 4, 2011 15:05
Fast Bucket Sort for Integers in JS
// Copyright 2011, Tom Switzer
// Under terms of ISC License: http://www.isc.org/software/license
/**
* Sorts an array of integers in linear time using bucket sort.
* This gives a good speed up vs. built-in sort in new JS engines
* (eg. V8). If a key function is given, then the result of
* key(a[i]) is used as the integer value to sort on instead a[i].
*
* @param a A JavaScript array.
@tixxit
tixxit / EditDistance.scala
Created September 28, 2011 03:10
Short Levenshtein distance implementation in Scala
package net.tixxit.levenshtein
import scala.math.min
object EditDistance {
def editDist[A](a: Iterable[A], b: Iterable[A]) =
((0 to b.size).toList /: a)((prev, x) =>
(prev zip prev.tail zip b).scanLeft(prev.head + 1) {
case (h, ((d, v), y)) => min(min(h + 1, v + 1), d + (if (x == y) 0 else 1))
}) last
@tixxit
tixxit / sm-on-osx.patch
Created November 23, 2011 18:59
Had to make some minor modifications to get SpiderMonkey 1.7.0 to compile on my computer (Mac OS X 10.6.8).
diff -rupN js/src/config/Darwin.mk js-mine/src/config/Darwin.mk
--- js/src/config/Darwin.mk 2007-02-05 11:24:49.000000000 -0500
+++ js-mine/src/config/Darwin.mk 2011-11-23 13:49:20.000000000 -0500
@@ -46,7 +46,7 @@
CC = cc
CCC = g++
CFLAGS += -Wall -Wno-format
-OS_CFLAGS = -DXP_UNIX -DSVR4 -DSYSV -D_BSD_SOURCE -DPOSIX_SOURCE -DDARWIN
+OS_CFLAGS = -DXP_UNIX -DSVR4 -DSYSV -D_BSD_SOURCE -DPOSIX_SOURCE -DDARWIN -DHAVE_VA_COPY
@tixxit
tixxit / pluck.sh
Created February 1, 2012 22:23
Bash script to output a fixed number of random lines from a file.
#!/bin/sh
#
# pluck - Pluck some random lines from a file.
#
# This simple tool chooses a fixed number of random lines from a file and
# outputs them to stdout (in order of their line number).
#
function usage {
@tixxit
tixxit / endoring.scala
Created April 10, 2012 18:12
Spire: Non-numeric Ring Example
package spire.example
import spire.math._
import Implicits._
object EndoRingExample {
// An abelian group is a commutative monoid with an inverse.
trait AbGroup[A] {
@tixxit
tixxit / fizzbuzz.scala
Created November 14, 2012 16:04
Compile time FizzBuzz in Scala.
package net.tixxit
import shapeless._
import Nat._
final class Fizz
final class Buzz
trait FizzBuzzAux[A <: Nat, B <: HList] {
def apply(): List[String]
import shapeless._
final class AnyRefCounter[L <: HList, N <: Nat]
object AnyRefCounter {
implicit def empty = new AnyRefCounter[HNil, Nat._0]
implicit def anyRef[H <: AnyRef, T <: HList, N <: Nat](implicit c: AnyRefCounter[T, N]) = new AnyRefCounter[H :: T, Succ[N]]
implicit def anyVal[H <: AnyVal, T <: HList, N <: Nat](implicit c: AnyRefCounter[T, N]) = new AnyRefCounter[H :: T, N]
def countRefs[L <: HList, N <: Nat](xs: L)(implicit c: AnyRefCounter[L, N], n: ToInt[N]): Int = n()
}