This file contains hidden or 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 insertion_sort(m, sort_by=(lambda a, b: a < b)): | |
def insert_item(m, item0): | |
for i, item in enumerate(m): | |
if sort_by(item0, item): | |
m.insert(i, item0) | |
return | |
m.append(item0) | |
if len(l) <= 1: | |
return l |
This file contains hidden or 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 selection_sort(m, sort_by=(lambda a, b: a < b)): | |
def find_next(m): | |
next_elem = m[0] | |
for i in m: | |
if sort_by(i, next_elem): | |
next_elem = i | |
return next_elem | |
if len(m) <= 1: # if list less than one element return | |
return m |
This file contains hidden or 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 merge_sort(m, sort_by): | |
def merge(left, right): | |
result = [] | |
while left and right: | |
# keep on merging/sorting from left/right | |
# lists on an element basis | |
if sort_by(left[0], right[0]): | |
result.append(left[0]) | |
left.pop(0) | |
else: |
This file contains hidden or 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 heap_sort(m, sort_by): | |
def sift_down(m, start, end): | |
"""Repair heap whose root element is at index start | |
assuming the heaps rooted at its children are valid""" | |
root = start | |
while 2*root+1 <= end: | |
child = 2*root + 1 # left child | |
swap = root |
This file contains hidden or 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 quick_sort(m, sort_by): | |
def partition(m, lo, hi): | |
p = m[hi] # choose a pivot | |
i = lo | |
for j in range(lo, hi): | |
if sort_by(m[j], p): | |
m[i], m[j] = m[j], m[i] | |
i += 1 | |
m[i], m[hi] = m[hi], m[i] | |
return i |
This file contains hidden or 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
class NQueensSquare(SearchProblem): | |
def __init__(self, N): | |
super(NQueensSquare, self).__init__() | |
self.N = N | |
self.initial_state = tuple([tuple([0 for i in range(N)]) for j in range(N)]) | |
self._actions = [(i, j) for i in range(N) for j in range(N)] | |
def actions(self, s): | |
'''Possible actions from a state.''' | |
# generate every possible state then filter out invalid |
This file contains hidden or 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
class NQueensRow(SearchProblem): | |
def __init__(self, N): | |
super(NQueensRow, self).__init__() | |
self.N = N | |
self.initial_state = () | |
self._actions = range(N) | |
shuffle(self._actions) # randomize actions | |
def actions(self, s): | |
'''Possible actions from a state.''' |
This file contains hidden or 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
class NQueensSwap(SearchProblem): | |
def __init__(self, N): | |
self.N = N | |
init = [i for i in range(N)] | |
shuffle(init) # randomize the initial array | |
self.initial_state = tuple(init) # states must me immutable | |
self._actions = [ (i, j) for i in range(N) | |
for j in range(i,N) | |
if i != j ] |
This file contains hidden or 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
'This is an example of list comprehension control variables being leaked to their outer scope' | |
for i in range(10): | |
a = [i for i in range(43)] | |
print i # will print 42 every time! |
This file contains hidden or 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 _viterbi(self, observations): | |
'''Generate the most likely sequence of words given the observations''' | |
transitions = self.transition_prob; emissions = self.emission_prob | |
V = {}; V[(0, X0)] = log10(1) # initial probabilities | |
path = {}; path[X0] = [X0] # path to most likely seq | |
current_words = [[] for i in range(len(observations)+1)] | |
current_words[0] = [X0] # list of words that have an entry in bigram table |
OlderNewer