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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / lcs.py
Created October 5, 2014 15:02
Python: longest common subsequence (recursive)
def lcs(s, t):
if not s or not t:
return ''
if s[0] is t[0]:
return s[0] + lcs(s[1:], t[1:])
result1 = lcs(s[1:], t)
result2 = lcs(s, t[1:])
if len(result1) > len(result2):
return result1
else:
@jleeothon
jleeothon / hw.py
Last active August 29, 2015 14:06
This is a module I run when I want to do probability homework
import locale
from math import factorial as f
locale.setlocale(locale.LC_ALL, 'en')
nice = lambda: locale.format("%d", _, grouping=True)
@jleeothon
jleeothon / ispaldrm.c
Last active August 29, 2015 14:06
C: is palindrome
#include <stdio.h>
#define palsize 100
/*
* Returns 1 if is a pure palindrome.
* The name is a bit mangled up because good C code shouldn't be understandable.
*/
int ispaldrm(char* s)
{