Skip to content

Instantly share code, notes, and snippets.

View jleeothon's full-sized avatar

Johnny Lee-Othon jleeothon

  • Wunderflats
  • Berlin
View GitHub Profile
@jleeothon
jleeothon / lca.rb
Created October 5, 2014 15:03
Ruby: lowest common ancestor (recursive)
def lcs s1, s2
if s1.empty? or s2.empty?
return ''
elsif s1[0] == s2[0]
return s1[0] + lcs(s1[1..-1], s2[1..-1])
end
left = lcs s1[1..-1], s2
right = lcs s1, s2[1..-1]
return left.size > right.size ? left : right
end
@jleeothon
jleeothon / dynlcs.py
Last active August 29, 2015 14:07
Python: recursive longest common subsequence (LCS) with dynamic programming
class DynamicLcs:
def __init__(self):
self.table = {}
@staticmethod
def run(s1, s2):
return DynamicLcs()._run(s1, s2)
def _run(self, s1, s2):
@jleeothon
jleeothon / addbinary.py
Last active August 29, 2015 14:07
Python: add binary numbers represented in a string recursively
def add_binary(n, m, c=False):
nb = n[-1] == "1" if n else False
mb = m[-1] == "1" if m else False
r = nb ^ mb ^ c
c = not nb and mb and c or nb and (mb or c)
n2 = n[:-1] if n else n
m2 = m[:-1] if m else m
if not n and not m:
return ""
return add_binary(n2, m2, c) + ("1" if r else "0")
@jleeothon
jleeothon / goodbyes.py
Last active August 29, 2015 14:08
Fancy exiting Python
farewell = lambda: exit.__call__("Fare well, good lover.")
# On Python 2
bravequit2 = lambda: type(exit)("The adventurously brave quitter")("I'll miss you. Please open the console when you be free again.")
# On Python 3
bravequit3 = lambda: type(exit)("The adventurously brave quitter", "")("I'll miss you. Please open the console when you be free again.")
@jleeothon
jleeothon / combs.rb
Last active August 29, 2015 14:08
Ruby combinatorics
class Integer
def !
if self > 0
(1..self).reduce :*
elsif self.zero?
1
else
raise ArgumentError, "No factorial for #{self}"
end
end
@jleeothon
jleeothon / stack.c
Created November 2, 2014 20:41
Simple stack in C
/* for any stack in the form... */
int _s[100];
int *s = _s; /* top of the pending stack (this location empty) */
/* be very careful about passing expressions for a stack, not well guarded */
#define isempty(s) ((s) - (_s))
#define push(s, v) (*s++ = (v))
#define pop(s) (*(--s))
#define peek(s) ((s)[-1])
@jleeothon
jleeothon / test.bat
Created November 24, 2014 23:52
Test an UVa problem written in C against sample input and compare with expected output
gcc "%1.c" -o "%1.o" && .\"%1.o" < "%1.in" > "%1.log" && fc "%1.log" "%1.out"
@jleeothon
jleeothon / urls.py
Last active August 29, 2015 14:12
CRUD URL Design with Python
# settings.py
APPEND_SLASH = True
# projectname/urls.py
from django.conf.urls import include
from django.conf.urls import patterns
from django.conf.urls import url
unsigned int fibonacci(unsigned int n) {
if (n > 2) {
unsigned int a = 1, b = 1;
while (n-- > 2) {
b = a + b;
a = b - a;
}
return b;
} else if (n) {
return 1;
@jleeothon
jleeothon / Preferences.sublime-settings
Created March 28, 2015 19:23
Sublime preferences
{
"font_size": 12,
"ignored_packages":
[
"Vintage"
],
"translate_tabs_to_spaces": true,
"rulers": [80],
"remember_open_files": false,
"hot_exit": false,