Skip to content

Instantly share code, notes, and snippets.

View DeaconDesperado's full-sized avatar

Mark Grey DeaconDesperado

  • Spotify
  • NY, New York
View GitHub Profile
import java.util.*;
import java.util.Map.*;
/**
* A template file with basic JUnit tests has been provided.
* Note that your program will be evaluated by a more comprehensive test suite.
*
* Please consider the runtime and space complexity of your algorithms and comment on those trade offs.
*
* Good luck!
@DeaconDesperado
DeaconDesperado / cherrypy_daemon.py
Last active August 12, 2016 23:57
Run cherrypy WSGI server as a daemon
from cherrypy.process.plugins import Daemonizer,PIDFile
import cherrypy
import argparse
parser = argparse.ArgumentParser(description="My server daemon")
parser.add_argument('-d','--daemon',help='Run the server as a daemon using the traditional double fork',action='store_true')
parser.add_argument('-a','--bind-address',help='Network interface to bind to',default='127.0.0.1')
parser.add_argument('-p','--port',help='Port to bind to',default=8080,type=int)
parser.add_argument('--pidfile',help='process id file',type=str)
args = parser.parse_args()
@DeaconDesperado
DeaconDesperado / inversions.py
Last active December 21, 2015 21:18
Merge sort with inversion counting
ints = [int(x) for x in open('IntegerArray.txt').read().split()]
def merge_and_count(a, b):
assert a == sorted(a) and b == sorted(b)
c = []
count = 0
i, j = 0, 0
while i < len(a) and j < len(b):
c.append(min(b[j], a[i]))
if b[j] < a[i]:
@DeaconDesperado
DeaconDesperado / heap.scala
Last active December 22, 2015 12:09
A trivial Scala max priority queue
package com.heap;
import util.control.Breaks._;
import collection.mutable._;
abstract class Heap(){
var table = ArrayBuffer[Char]();
var N:Int = -1;
def +=(value:Char) = {
N = N+1
@DeaconDesperado
DeaconDesperado / circ.py
Created September 6, 2013 22:39
Circular Arrays
cases = [
([2,2,-1],True),
([3,1,2,-2],True),
([-4,2,4],False),
([9,8,8],False)
]
def is_circular_complete(case):
key = 0
size = len(case)
@DeaconDesperado
DeaconDesperado / Bloom.scala
Last active January 30, 2017 10:18
Trivial bloom filter implementation in Scala.
class BloomSet[A] private (val size: Int, val k: Int,private val contents: Vector[Boolean]){
val width = contents.length
def this(width: Int, k: Int) = this(0, k, BloomSet.alloc(width))
def contains(e:Any) = {
(0 to k).foldLeft(true) { (acc,i) =>
acc && contents(hash(e,i,width))
}
@DeaconDesperado
DeaconDesperado / sieve.py
Created September 20, 2013 17:30
Pythonic Sieve of Eratosthenes
sieve = [x for x in range(2,i) if not [t for t in range(2,x) if not x%t]]
@DeaconDesperado
DeaconDesperado / dummy.py
Last active December 23, 2015 18:29
Python conditional truthiness is len by default
class Dummy(object):
def __init__(self,foo):
self.foo = foo
def __len__(self):
#This will make me always falsy, define and __nonzero__ method!
return 0
def __gt__(self,other):
class Node(object):
def __init__(self, key, parent=None):
self.key = key
self.parent = parent
self.left = None
self.right = None
def has_children(self): return len(self) > 0
def delete(self, node):
@DeaconDesperado
DeaconDesperado / knapsack.py
Created September 24, 2013 15:45
Knapsack algorithm
import math
from random import random
import collections
import functools
import logging
import sys
try:
level = sys.argv[1]
loglevel = getattr(logging,level)