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
module Main (main) where | |
match :: String -> String -> Bool | |
match ('^':rest) text = matchLocal rest text | |
match rex (c:rest) = matchLocal rex (c:rest) || match rex rest | |
match _ _ = False | |
matchLocal :: String -> String -> Bool | |
matchLocal [] _ = True | |
matchLocal "$" [] = True |
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 pairify(it): | |
it0, it1 = itertools.tee(it, 2) | |
first = next(it0) | |
return zip(itertools.chain([first, first], it0), it1) | |
def cluster(sequence, maxgap): | |
batch = [] | |
for prev, val in pairify(sequence): | |
if abs(val - prev) >= maxgap: | |
yield batch |
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
# A simple discrete-time PID controller. If plot is true, this will use matplotlib to show a graph. | |
# Otherwise, it will simply print out the successive values of the controller. | |
plot = True | |
if __name__ == '__main__': | |
if plot: | |
import matplotlib.pyplot as plt | |
v = [0] * 100 |
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
from collections import defaultdict | |
import random | |
class Markov: | |
memory = defaultdict(list) | |
separator = ' ' | |
def get_initial(self): | |
return (' ', ' ') |
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
from mpl_toolkits.basemap import Basemap | |
from shapely.geometry import Point, MultiPoint | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
x = [] | |
y = [] | |
with open('data.tsv', 'r') as fp: | |
for line in fp: | |
s = line.split('\t') |
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 postfix(tokens): | |
output_stack = [] | |
op_stack = [] | |
for token in tokens: | |
if is_number(token): | |
output_stack.append(token) | |
elif token in ['+', '-', '*', '/']: | |
while len(op_stack) > 0: | |
if (token in ['+', '-'] and op_stack[-1] in ['*', '/']) or (token in ['*', '/'] and op_stack[-1] in ['*', '/']): | |
output_stack.append(op_stack.pop()) |
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 build(pairs, root=None): | |
tree = {} | |
for (child, parent) in pairs: | |
if parent == root: | |
tree[child] = build(filter(lambda x: x != (child, parent), pairs), child) | |
return tree | |
print build([("H", "G"), ("F", "G"), ("G", "D"), ("E", "D"), ("A", "E"), ("B", "C"), ("C", "E"), ("D", None)]) |
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 quicksort(a): | |
if len(a) <= 1: | |
return a | |
lesser = [x for x in a[1:] if x < a[0]] | |
greater = [x for x in a[1:] if x >= a[0]] | |
return quicksort(lesser) + [a[0]] + quicksort(greater) | |
print quicksort([4, 6, 5, 3, 4, 7, 6, 5, 8, 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
def merge(a1, a2): | |
res = [] | |
i = 0 | |
j = 0 | |
while i < len(a1) and j < len(a2): | |
if a1[i] < a2[j]: | |
res.append(a1[i]) | |
i += 1 | |
else: | |
res.append(a2[j]) |
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 sublists(a, n): | |
if n == 0: | |
return [[]] | |
elif len(a) == 0: | |
return [] | |
else: | |
return sublists(a[1:], n) + [[a[0]] + s for s in sublists(a[1:], n - 1)] | |
def partition(a): |
OlderNewer