This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
OlderNewer