Skip to content

Instantly share code, notes, and snippets.

@ameerkat
ameerkat / BoyerMooreStringSearch.py
Created October 14, 2010 17:46
Boyer Moore string search implementation in Python
# Boyer Moore String Search implementation in Python
# Ameer Ayoub <ameer.ayoub@gmail.com>
# Generate the Bad Character Skip List
def generateBadCharShift(term):
skipList = {}
for i in range(0, len(term)-1):
skipList[term[i]] = len(term)-i-1
return skipList
@ameerkat
ameerkat / sorts.py
Created December 7, 2010 15:04
Various sorts implemented in Python (Insertion, Bubble, Merge, Quick, Counting)
# BunchO Sorting Algorithms
# Ameer Ayoub <ameer.ayoub@gmail.com>
# Last Modified 12/2/2010
import random
#
# Insertion Sort
#
def insertion_sort(l):
"""Given an input list, returns a sorted permutation of the list
@ameerkat
ameerkat / order_statistics.py
Created December 10, 2010 21:04
Order statistics using randomized pivot
# Order Statistics
# Ameer Ayoub <ameer.ayoub@gmail.com>
import random, copy
# From quicksort
# Nonrandomized Pivot
def partition(l, p, q, pivot=None):
r = copy.copy(l)
if pivot:
r[pivot], r[p] = r[p], r[pivot]
@ameerkat
ameerkat / euler39brute.py
Created April 5, 2011 03:50
Brute force attempt at project euler #39
def euler39brute():
sum_count = [0] * 1001
for a in range(2000/3 + 1):
if a%100 == 0:
print "|",
for b in range(a+1):
for c in range(a+1,1001-a-b):
if a**2 + b**2 == c**2:
sum_count[a+b+c] += 1
return sum_count.index(max(sum_count))
@ameerkat
ameerkat / euler39betterorworse.py
Created April 5, 2011 03:59
Attempt 2 at project euler #39, using some gained insight
from fractions import gcd
def euler39betterorworse():
sum_count = [0] * 1001
triples_to_check = []
for a in range(2000/3):
if a%100 == 0:
print "|",
for b in range(a+1):
for c in range(a+1,1001-a-b):
@ameerkat
ameerkat / euler39better2.py
Created April 5, 2011 04:09
Project euler #39, attempt 3 using some facts about the parity of a and b
from fractions import gcd
def euler39better2():
sum_count = [0] * 1001
triples_to_check = []
for a in range(2000/3):
if a%100 == 0:
print "|",
for b in range((a%2)+1, a+1, 2):
for c in range(a+1,1001-a-b):
@ameerkat
ameerkat / euler39optimized.py
Created April 5, 2011 04:28
Fully optimized euler #39!
from fractions import gcd
def euler39optimized():
sum_count = [0] * 1001
y = 2
while y**2 <= 500:
for z in [x for x in range(y) if gcd(y, x) == 1]:
a = y**2 - z**2
b = 2*y*z
c = y**2 + z**2
@ameerkat
ameerkat / inc_v_num.py
Created May 19, 2011 01:20
Increment version number in drupal module info file
# increment_version_num.py
# Utility to incremement version number in drupal info files
# Ameer Ayoub <ameer.ayoub@gmail.com>
import re, sys, os
major_v = 0
minor_v = 0
def inc_dv_num(s, minor = True):
global major_v, minor_v
@ameerkat
ameerkat / gist:979979
Created May 19, 2011 01:26
Pre-commit hook for drupal version increment script
#!/bin/sh
echo updating version numbering...
python inc_v_num.py --file content_reservation.info
@ameerkat
ameerkat / hc.py
Created June 10, 2011 08:05
Python html linker
# Python HTML Linker
# Ameer Ayoub <ameer.ayoub@gmail.com>
from sys import argv
import re
def print_usage(script_name):
print "usage:", script_name, "target_file template_file source_file0 (source_file1, source_file2, ...)"
if __name__ == "__main__":
script_name = argv[0]