Skip to content

Instantly share code, notes, and snippets.

View kedarbellare's full-sized avatar

Kedar Bellare kedarbellare

View GitHub Profile
@kedarbellare
kedarbellare / ThresholdLevenshtein.scala
Created September 14, 2011 15:14
Scala implementation of threshold edit distance
def thresholdLevenshtein(_s: String, _t: String, threshold: Int): Int = {
val (s, t) = if (_s.length > _t.length) (_s, _t) else (_t, _s)
val slen = s.length
val tlen = t.length
var prev = Array.fill[Int](tlen+1)(Int.MaxValue)
var curr = Array.fill[Int](tlen+1)(Int.MaxValue)
for (n <- 0 until math.min(tlen+1, threshold+1)) prev(n) = n
for (row <- 1 until (slen+1)) {
@kedarbellare
kedarbellare / sudoku_solver.py
Created July 12, 2012 03:39
Sudoku checker and solver for CS258
hard = [[1,0,0,0,0,7,0,9,0],
[0,3,0,0,2,0,0,0,8],
[0,0,9,6,0,0,5,0,0],
[0,0,5,3,0,0,9,0,0],
[0,1,0,0,8,0,0,0,2],
[6,0,0,0,0,4,0,0,0],
[3,0,0,0,0,0,0,1,0],
[0,4,0,0,0,0,0,0,7],
[0,0,7,0,0,0,3,0,0]]
@kedarbellare
kedarbellare / sudoku.py
Created July 13, 2012 05:44
Complete sudoku solver
# CHALLENGE PROBLEM:
#
# Use your check_sudoku function as the basis for solve_sudoku(): a
# function that takes a partially-completed Sudoku grid and replaces
# each 0 cell with a number in the range 1..9 in such a way that the
# final grid is valid.
#
# There are many ways to cleverly solve a partially-completed Sudoku
# puzzle, but a brute-force recursive solution with backtracking is a
# perfectly good option. The solver should return None for broken
@kedarbellare
kedarbellare / sudoku_solve.py
Created July 13, 2012 13:57
Sudoku solver/checker
def times(A, B):
return [(i,j) for i in A for j in B]
dims = range(9)
subdims = ([0,1,2],[3,4,5],[6,7,8])
squares = times(dims, dims)
unitlist = ([times(dims, [c]) for c in dims] +
[times([r], dims) for r in dims] +
[times(rs, cs) for rs in subdims for cs in subdims])
units = dict((s, [u for u in unitlist if s in u])
@kedarbellare
kedarbellare / sudoku_utils.py
Created July 13, 2012 14:37
Utility for sudoku
## Tests
import time, random
def from_file(filename, sep='\n'):
"Parse a file into a list of strings, separated by sep."
return file(filename).read().strip().split(sep)
def parse_grid(gridstr):
gridstr = gridstr.replace('\n', '')
return [[int(gridstr[i+9*j]) for i in range(9)] for j in range(9)]
@kedarbellare
kedarbellare / centrality.py
Created July 18, 2012 03:11
Finding the max and average centrality in a graph
#
# Write centrality_max to return the maximum distance
# from a node to all the other nodes it can reach
#
from collections import deque
def centrality_max(G, v):
open_list = deque([v])
distance = {v: 0}
@kedarbellare
kedarbellare / pdf_fuzz.py
Created July 20, 2012 02:26
PDF Fuzzer
file_list = ["10.1.1.111.1781.pdf", "10.1.1.111.5264.pdf", "10.1.1.39.1596.pdf", "10.1.1.41.8589.pdf", "10.1.1.42.5619.pdf"]
apps = [
"/Applications/Adobe Reader 9/Adobe Reader.app/Contents/MacOS/AdobeReader",
"/Applications/Adobe Reader.app/Contents/MacOS/AdobeReader",
"/Applications/Preview.app/Contents/MacOS/Preview"]
fuzz_output = "fuzz.pdf"
FuzzFactor = 250
@kedarbellare
kedarbellare / sage_terminal_velocity.py
Created July 22, 2012 21:41
Sage (terminal velocity of different objects)
## Credits:
##
## Collaborative effort:
## - Michel Dusseault wrote the code.
## - I worked out some of the differential equations.
##
var('t')
g = 9.81 # m/s^2
rho = 1.2 # Rho: Density of air at sea level @ 25C
@kedarbellare
kedarbellare / rps.py
Created July 24, 2012 06:59
Rock paper scissors
def match(player1, player2):
moves1 = []
moves2 = []
wins1 = 0
wins2 = 0
for i in xrange(1000):
move1 = player1(moves1, moves2)
move2 = player2(moves2, moves1)
#print move1, move2
moves1.append(move1)
@kedarbellare
kedarbellare / dijkstra_fast.py
Created August 1, 2012 22:43
dijkstra implementations
import heapq
import csv
# no heapq implementation
def up_heapify(L, i):
while i:
pi = (i-1)/2
if L[pi] > L[i]:
L[i], L[pi] = L[pi], L[i]
i = pi