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
{-# LANGUAGE ExistentialQuantification #-} | |
module DFA ( | |
DFA, | |
runDFA, | |
scanDFA, | |
isAccepting, | |
) where | |
import Data.Set (Set) |
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 Text.Parsec | |
import Text.Parsec.Expr | |
import Text.Parsec.Combinator | |
import Data.Functor | |
data Exp = Num Int | |
| Add Exp Exp | |
| Sub Exp Exp | |
| Mul Exp Exp | |
| Div Exp Exp |
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 qualified Data.Vector as V | |
import qualified Data.List as L | |
accSum :: Num t => [t] -> [t] | |
accSum xs = aux xs 0 | |
where aux [] a = [] | |
aux (x:xs') a = (a + x) : aux xs' (a + x) | |
lowerBound :: (Num a, Ord a) => V.Vector a -> a -> Int |
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 Data.List.Split | |
import qualified Data.Vector as V | |
rmqBuild :: [Int] -> (Int, V.Vector Int) | |
rmqBuild xs = | |
let l = length xs | |
n = until (\ x -> x >= l) (*2) 1 | |
inf = maxBound :: Int | |
f ys = map minimum $ chunksOf 2 ys | |
in (n ,V.fromList $ concat $ |
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
#!/usr/local/Gambit-C/bin/gsi | |
; Copyright (C) 2004 by Marc Feeley, All Rights Reserved. | |
; This is the "90 minute Scheme to C compiler" presented at the | |
; Montreal Scheme/Lisp User Group on October 20, 2004. | |
; Usage with Gambit-C 4.0: | |
; | |
; % ./90-min-scc.scm test.scm |
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 operator | |
Symbol = str | |
def tokenize(exp): | |
return exp.replace('(', ' ( ').replace(')', ' ) ').split() | |
def parser(exp): | |
return read_from_tokens(tokenize(exp)) | |
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 random | |
def merge_sort(xs): | |
"""Inplace merge sort of array without recursive. The basic idea | |
is to avoid the recursive call while using iterative solution. | |
The algorithm first merge chunk of length of 2, then merge chunks | |
of length 4, then 8, 16, .... , until 2^k where 2^k is large than | |
the length of the array | |
""" | |
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 operator import add, sub | |
import sys | |
def max_subarray(arr): | |
ret, cur = arr[0], 0 if arr[0] < 0 else arr[0] | |
for num in arr[1:]: | |
ret = max(ret, cur + num) | |
cur = max(0, cur + num) | |
return ret |
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 sys | |
import itertools | |
class RMQ: | |
def __init__(self, n): | |
self.sz = 1 | |
self.inf = (1 << 31) - 1 | |
while self.sz <= n: self.sz = self.sz << 1 | |
self.dat = [self.inf] * (2 * self.sz - 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
import sys | |
import itertools | |
def extend_gcd(a, b): | |
if b == 0: | |
return (1, 0, a) | |
else: | |
(a1, b1, g) = extend_gcd(b, a % b) | |
return (b1, a1 - (a // b) * b1, g) | |