Skip to content

Instantly share code, notes, and snippets.

View stevekrenzel's full-sized avatar

Steve Krenzel stevekrenzel

View GitHub Profile
@stevekrenzel
stevekrenzel / gist:1505619
Created December 21, 2011 10:59
Instagram challenge solution
unshred = (shreddedImage, shredWidth) ->
{width, height} = shreddedImage
shreds = split shreddedImage, shredWidth
weights = cross shreds, (x, y) ->
if x == y
0
else
mean zipWith unzip(rightEdge(x)), unzip(leftEdge(y)), correlate
@stevekrenzel
stevekrenzel / gist:1088119
Created July 17, 2011 21:51
Fancy Table (for Sab)
fancyTable = lambda table: '\n'.join("%%%ss " * min(map(len, table)) % tuple(max(map(len, x)) for x in zip(*table)) % tuple(x)[:min(map(len, table))] for x in table)
# You can try it by opening python and running:
# >>> fancyTable = lambda table: '\n'.join("%%%ss " * min(map(len, table)) % tuple(max(map(len, x)) for x in zip(*table)) % tuple(x)[:min(map(len, table))] for x in table)
# >>> print fancyTable([['Text', '1', 'a'], ['Longer Text', '123', 'abc'], ['Even longer text!', '123456789', 'abcdefghi']])
# Text 1 a
# Longer Text 123 abc
# Even longer text! 123456789 abcdefghi
@stevekrenzel
stevekrenzel / gist:1046332
Created June 25, 2011 10:05
Longest palindrome in C++ (fast version)
#include <iostream>
#include <string>
using namespace std;
string findLongestPalindrome(string str) {
int max_lower = 0, max_upper = 0, len = str.length(), lower = 0, upper = 0;
for(int i = 0; i < len; i++) {
upper = i + 1, lower = i;
for(int j = 0; j < 2; j++) {
lower = i - j;
@stevekrenzel
stevekrenzel / gist:1042187
Created June 23, 2011 09:00
Longest palindrome in C++ (readable version)
#include <iostream>
#include <string>
using namespace std;
#define MAX_STR(a, b) (((a.length()) > (b.length())) ? (a) : (b))
string getPalindromeAt(string str, int lower, int upper) {
for(;lower >= 0 and upper < str.length() and str[lower] == str[upper]
;upper++, lower--);
return str.substr(lower + 1, upper - lower - 1);
@stevekrenzel
stevekrenzel / gist:845969
Created February 27, 2011 06:49
Find the longest common substring between two strings.
from collections import defaultdict
def longest_common_substring(a, b):
index = defaultdict(list)
for i, c in enumerate(b):
index[c].append(i)
mxi, mxj, mxsz = 0, 0, 0
matches = defaultdict(int)
for i, c in enumerate(a):
new_matches = defaultdict(int)