Skip to content

Instantly share code, notes, and snippets.

@m00nlight
m00nlight / DFA.hs
Last active August 29, 2015 14:18 — forked from romac/DFA.hs
{-# LANGUAGE ExistentialQuantification #-}
module DFA (
DFA,
runDFA,
scanDFA,
isAccepting,
) where
import Data.Set (Set)
@m00nlight
m00nlight / gist:2868363ec217f97072b4
Created March 30, 2015 09:15
Simple Haskell arithmetic parser and evaluator
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
@m00nlight
m00nlight / gist:18df8064dda4fcd3bf8c
Created March 14, 2015 15:33
Haskell lowerBound and upperBound
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
@m00nlight
m00nlight / gist:117e44a2c8cbf8b45c09
Last active August 29, 2015 14:16
Haskell Range Minimum Query
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 $
#!/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
@m00nlight
m00nlight / gist:0c378d9a7246a02b4650
Last active August 29, 2015 14:15
Python S-exp arithmetic evaluation
import operator
Symbol = str
def tokenize(exp):
return exp.replace('(', ' ( ').replace(')', ' ) ').split()
def parser(exp):
return read_from_tokens(tokenize(exp))
@m00nlight
m00nlight / gist:a076d3995406ca92acd6
Last active October 21, 2020 21:31
Python merge sort in place, so space complexity is O(1)
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
"""
@m00nlight
m00nlight / gist:34b0f6a2d6a26fcef930
Created February 19, 2015 07:23
Python max sub-array problem
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
@m00nlight
m00nlight / gist:1f226777a49cfc40ed8f
Last active March 7, 2022 12:24
Python range minimum query
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)
@m00nlight
m00nlight / gist:56430f21456a23f19ed9
Created February 14, 2015 13:13
Python extend greatest common divisor
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)