Skip to content

Instantly share code, notes, and snippets.

@kflu
kflu / findmajority.py
Created March 8, 2012 06:34
This code implements the problem of "Selecting M from N elements that appear more than M/N times". For more details, see http://wp.me/pL72j-5E
import unittest
def FindMajority(array, M):
'''Find elements that occurs more than(>) N/M times in the array of size N
array: the array that is to be scanned through for elements returns a
dictionary with keys set to elements that occur more than(>) N/M times in
the array of size N. The key's values are their number of occurrences.
'''
@kflu
kflu / quicksort.py
Created March 16, 2012 05:08
Quicksort implementation according to CLRS
'''Quicksort implementation according to CLRS'''
import unittest
def _swap(array, i, j):
tmp = array[i]
array[i] = array[j]
array[j] = tmp
def _partition(array, lower, upper):
@kflu
kflu / longest_increasing_subsequence.py
Created April 27, 2012 03:00
(deprecated in favor of https://gist.github.com/2723571) Algorithm to find the longest increasing subsequence (http://people.csail.mit.edu/bdean/6.046/dp/)
class LIS:
def __init__(self, A):
self.A = A
self.N = len(A)
self.table = self.init_table()
def init_table(self):
table = [[0,-1] for i in xrange(self.N)]
table[0][0] = 1
return table
@kflu
kflu / max_sum_seq.py
Created May 15, 2012 06:42
Maximum Value Contiguous Subsequence. Given a sequence of n real numbers A(1) ... A(n), determine a contiguous subsequence A(i) ... A(j) for which the sum of elements in the subsequence is maximized. http://people.csail.mit.edu/bdean/6.046/dp/
import pytest
class MaxSumSeq:
def __init__(self, A):
assert A
self.A = A
self.n = len(A)
self.tab = [None for i in range(self.n)]
self.tab[0] = self.A[0]
@kflu
kflu / coinchanger.py
Created May 17, 2012 05:29
Changing the bill with minimum number of coins
"""Changing the bill with minimum number of coins."""
class EmptyVError(Exception): pass
class CoinChanger:
def __init__(self, C, v):
if not v: raise EmptyVError
self.C = C
self.v = v
@kflu
kflu / longest_inc_seq.py
Created May 18, 2012 06:32
Find the longest increasing subsequence.
"""Find the longest increasing subsequence.
Given a sequence of n real numbers A(1) ... A(n), determine a subsequence (not
necessarily contiguous) of maximum length in which the values in the
subsequence form a strictly increasing sequence.
[http://people.csail.mit.edu/bdean/6.046/dp/]
Optimal subproblem:
@kflu
kflu / boxstacking.py
Created May 19, 2012 23:29
Box stacking with the maximum height
"""Box stacking with the maximum height.
Box Stacking. You are given a set of n types of rectangular 3-D boxes, where
the i^th box has height h(i), width w(i) and depth d(i) (all real numbers). You
want to create a stack of boxes which is as tall as possible, but you can only
stack a box on top of another box if the dimensions of the 2-D base of the
lower box are each strictly larger than those of the 2-D base of the higher
box. Of course, you can rotate a box so that any side functions as its base. It
is also allowable to use multiple instances of the same type of box.
@kflu
kflu / buildingbridges.py
Created May 20, 2012 05:44
Building Bridges
'''Building Bridges
Building Bridges. Consider a 2-D map with a horizontal river passing through
its center. There are n cities on the southern bank with x-coordinates a(1) ...
a(n) and n cities on the northern bank with x-coordinates b(1) ... b(n). You
want to connect as many north-south pairs of cities as possible with bridges
such that no two bridges cross. When connecting cities, you can only connect
city i on the northern bank to city i on the southern bank.
http://people.csail.mit.edu/bdean/6.046/dp/
@kflu
kflu / balanced_partition.py
Created May 27, 2012 05:49
Balanced partition
""" Balanced partition
You have a set of n integers each in the range 0 ... K. Partition these
integers into two subsets such that you minimize |S1 - S2|, where S1 and S2
denote the sums of the elements in each of the two subsets.
http://people.csail.mit.edu/bdean/6.046/dp/
"""
@kflu
kflu / EditDistance.cs
Created May 30, 2012 16:26
Calculate the edit distance
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using SimpleTest;
namespace EditDistance
{
class Program