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 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 |
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 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 |
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 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 |
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 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: |
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 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 |
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 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] |
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 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), |
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 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 |
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 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) |
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 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) |