Skip to content

Instantly share code, notes, and snippets.

@ameerkat
ameerkat / 2012_C.py
Created April 17, 2012 00:42
2012 Code Jam Qualifier C
def rotate(n, i):
j = 0
s = str(n)
while(j < i):
s = s[-1] + s[:-1]
j += 1
return int(s)
def rn(n, m):
count = 0
@ameerkat
ameerkat / 2012_B.py
Created April 17, 2012 00:42
2012 Code Jam Qualifier B
def gen_trip(n, p):
flag = False
trips = []
for i in range(0,11):
for j in range(0, 11):
for k in range(0, 11):
if i + j + k == n:
if(max([i,j,k]) - min([i,j,k]) <= 2 and max([i,j,k]) >= p):
trips.append([i, j, k])
if(max([i,j,k]) - min([i,j,k]) <= 1):
@ameerkat
ameerkat / 2012_A.py
Created April 17, 2012 00:41
2012 Code Jam Qualifier A
d = {}
s1 = "yeqz ejp mysljylc kd kxveddknmc re jsicpdrysi rbcpc ypc rtcsra dkh wyfrepkym veddknkmkrkcd de kr kd eoya kw aej tysr re ujdr lkgc jv"
s2 = "aozq our language is impossible to understand there are twenty six factorial possibilities so it is okay if you want to just give up"
for ch in range(len(s1)):
d[s1[ch]] = s2[ch]
f = open("A.in")
t = int(f.readline().strip())
for i in range(1,t+1):
print "Case #"+ str(i) + ": " + ''.join([(d[ch] if ch in d else ch) for ch in f.readline()]),
@ameerkat
ameerkat / ubgd.py
Created February 16, 2012 21:51
Univariate Batch Gradient Descent
# Univariate Batch Gradient Descent - Linear Regression
# Derived from Andrew Ng's Lectures on Machine Learning (Lecture 2)
# Ameer Ayoub <ameer.ayoub@gmail.com> 2/16/2012
import math
import matplotlib.pyplot as plt
def pd_sqr_err0(theta0, theta1, x, y):
m = len(x)
return sum([(1.0/m)*((theta0 + theta1 * x[i]) - y[i]) for i in range(m)])
@ameerkat
ameerkat / splitter.py
Created November 27, 2011 01:11
scanned page splitter
from PIL import Image
import math, os, sys
def calculate_seam(im, band_width, middle_bias = True, middle_bias_strength = 2):
pix = im.load()
size = im.size
max_std_dev = 0
max_col = 1
for i in range(band_width, im.size[0]-band_width+1):
multiplier = (1-(4*math.sqrt((i-im.size[0]/2)**2)/im.size[0]/2))**middle_bias_strength
@ameerkat
ameerkat / IMDBRegex.py
Created August 10, 2011 09:54
Regex for parsing out information from the IDMB list files
name = re.compile("""
('.+')?\s* # nickname (optional, group 1)
(([^,']*),)?\s* # last name (optional, group 3)
([^\(]+) # first name (required, group 4)
(\((\w+)\))? # actor number (optional, group 6)
""", re.VERBOSE)
acted_in = re.compile("""
"?([^"]*?)"?\s # title (required, group 1) surrounded by quotations if it's a tv show
\(((\d+)/?(\w+)?).*?\) # the year (required, group 3), followed by `/ROMAN_NUMERAL`
# (optional, group 4) if multiple in same year
@ameerkat
ameerkat / header_generator.py
Created July 10, 2011 02:27
Templatized header generator for a cpp project
from sys import argv
import re
def header_def_name(camelcase):
word = ""
for ch in camelcase:
if ch.isupper() and len(word) > 0:
word += "_"
word += ch
return word.upper()
@ameerkat
ameerkat / generic_header.tpl
Created July 10, 2011 02:33
Generic header for use with header_generator.py
/*
* Copyright 2011 Ameer Ayoub
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
@ameerkat
ameerkat / fib3.cpp
Created June 14, 2011 21:39
Calculating Fibonacci numbers with matrices
/* Fibonacci Calculation using Matrices
* Ameer Ayoub <ameer.ayoub@gmail.com>
* Jun 13, 2011
*/
#include <iostream>
#include <math.h>
using namespace std;
const int fib_matrix_size = 2;
@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]