This file contains 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
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) |
This file contains 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
#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); |
This file contains 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
#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; |
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
#!/usr/bin/env python | |
from urllib import urlopen | |
word_list = urlopen('http://pastebin.com/raw.php?i=jSD873gL').read().split() | |
scrambled_words = ['mkeart', 'sleewa', 'edcudls', 'iragoge', 'usrlsle', | |
'nalraoci', 'nsdeuto', 'amrhat', 'inknsy', 'iferkna'] | |
for scrambled in sorted(scrambled_words, key=len): | |
matches = [word for word in word_list if sorted(scrambled) == sorted(word)] |
This file contains 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
caesar_encode = caesar_decode = lambda m: m.encode('rot13') |
This file contains 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
#!/usr/bin/env python | |
from string import printable, maketrans | |
from random import seed, shuffle | |
def cipher_alphabet(key): | |
alphabet = [c for c in printable] | |
seed(hash(key)) | |
shuffle(alphabet) | |
return ''.join(alphabet) |
This file contains 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
#!/usr/bin/env python | |
lower, upper = 1, 100 | |
print "Guess a number between %s and %s (inclusive)." % (lower, upper) | |
while lower < upper: | |
mid = (lower + upper) / 2 | |
guess = raw_input("Is your number higher than %s? (y/n): " % (mid)) | |
if guess == 'y': |
This file contains 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
from sys import argv, exit | |
types = { | |
'simple': lambda capital, rate, time: capital * (1 + (rate * time)), | |
'compound': lambda capital, rate, time: capital * ((1 + rate) ** time) | |
} | |
if len(argv) != 5 or argv[1] not in types: | |
print "usage: %s <%s> capital rate time" % (argv[0], '|'.join(types)) | |
exit(1) |
OlderNewer