Skip to content

Instantly share code, notes, and snippets.

@globby
globby / Mersenne.py
Created March 5, 2014 03:57
An implementation of the Mersenne Twister Pseudorandom Generator
class MersenneTwister(object):
def __init__(self,seed):
self.MT = range(624)
self.index = 0
self.initialize(seed)
def last32(self,num):
return int(bin(num)[-32:],2)
def initialize(self,seed):
self.index = 0
self.MT[0] = seed
@globby
globby / BalancedBrackets.py
Created March 5, 2014 03:24
Algorithm to check balanced brackets
def BalancedBrackets(string):
a = 0
for x in string:
a += 1 if x == '[' else -1 if x == ']' else 0
if a < 0: return False
return not a
@globby
globby / BinarySearch.py
Created March 5, 2014 02:51
An implementation of an iterative Binary Search
def BinarySearch(lst,key):
low, high = 0, len(lst)
while low <= high:
m = (low + high) / 2
if lst[m] > key:
high = m - 1
elif lst[m] < key:
low = m + 1
else:
return m
@globby
globby / BinarySearch.py
Created March 5, 2014 02:42
An implementation of a recursive Binary Search
def BinarySearch(lst, key):
def Recurse(lst,key,low,high):
if high < low:
return -1
m = (low + high) / 2
if lst[m] > key:
return Recurse(lst,key,low,m-1)
elif lst[m] < key:
return Recurse(lst,key,m+1,high)
else:
@globby
globby / RabinKarp.py
Created March 4, 2014 03:15
An implementation of the Rabin-Karp algorithm
def RabinKarp(string, sub):
n, m = len(string), len(sub)
for i in range(0,n-m+1):
for j in range(0,m):
if string[i+j] != sub[j]:
break
else:
return i
return -1
@globby
globby / KMP.py
Created March 4, 2014 03:05
An implementation of the Knuth-Morris-Pratt Algorithm
def table(sub):
pos, cnd = 2, 0
T = [-1,0]
while pos < len(sub):
if sub[pos-1] == sub[cnd]:
cnd += 1
pos += 1
T.insert(pos,cnd)
elif cnd > 0:
cnd = T[cnd]
@globby
globby / Verhoeff.py
Created March 4, 2014 01:23
A slightly LESS confusing version of the Verhoeff algorithm
def verhoeff(string):
string, c = list(string)[::-1], 0
d = (
(0,1,2,3,4,5,6,7,8,9),
(1,2,3,4,0,6,7,8,9,5),
(2,3,4,0,1,7,8,9,5,6),
(3,4,0,1,2,8,9,5,6,7),
(4,0,1,2,3,9,5,6,7,8),
(5,9,8,7,6,0,4,3,2,1),
(6,5,9,8,7,1,0,4,3,2),
@globby
globby / Verhoeff.py
Created March 4, 2014 01:19
A slightly confusing implementation of the Verhoeff algorithm
def verhoeff(string):
string, c = list(string)[::-1], 0
d, p, inv = map(lambda x:(lambda f,s:[map(int,list(f[i:i+s])) for i in range(0,len(f),s)])("0"+str(int(x,32)),10),
["1pphkblhcil6aq7anvo920aj7hpi14o8nu4t71vvat8a270c2avapk1868t7300dna",
"labkl84c4rp10j7aqnspj1hi8k79r7o790ac4n72ok70e8uale7a",
"cs4c3l"])
for i, v in enumerate(string):
c = d[c][p[i%8][int(v)]]
return c == 0
@globby
globby / Fletcher.py
Created March 4, 2014 00:35
The Fletcher16, Fletcher32 and Fletcher64 algorithms
def Fletcher16(string):
a = map(ord,string)
b = [sum(a[:i])%255 for i in range(len(a)+1)]
return (sum(b) << 8) | max(b)
def Fletcher32(string):
a = map(ord,string)
b = [sum(a[:i])%65535 for i in range(len(a)+1)]
return (sum(b) << 16) | max(b)
def Fletcher64(string):
a = map(ord,string)
@globby
globby / Quicksort.py
Created March 3, 2014 23:44
The Quicksort algorithm
def quicksort(a):
if len(a) <= 1: return a
pivot = a.pop(len(a)/2)
less, greater = [], []
for x in a:
if x <= pivot:
less.append(x)
else:
greater.append(x)
return quicksort(less) + [pivot] + quicksort(greater)