Skip to content

Instantly share code, notes, and snippets.

@brendanberg
brendanberg / gist:633b62c49bb20219ca1d
Last active August 29, 2015 14:06
Python's dateutil module, everybody!
>>> from dateutil import parser
>>> parser.parse('0001-01-01')
datetime.datetime(2001, 1, 1, 0, 0)
>>> parser.parse('0010-01-01')
datetime.datetime(2001, 10, 1, 0, 0)
>>> parser.parse('0015-01-01')
datetime.datetime(2001, 1, 15, 0, 0)
@brendanberg
brendanberg / vowlies.py
Created August 6, 2010 02:26
Counts vowels and consonants from stdin
import sys, string
def vowels(str):
count = 0
for let in str:
if let in list("AEIOUaeiou"):
count += 1
return count
def vowelies(str):
@brendanberg
brendanberg / lisp.coffee
Created August 24, 2011 07:11
Lisp list functions in CoffeeScript
cons = (h, t) -> (m) -> m(h, t)
car = (x) -> x((h, t) -> h)
cdr = (x) -> if x then x((h, t) -> t) else null
map = (ls, f) ->
cons (f car ls), if cdr ls then map cdr(ls), f else null
foldl = (ls, f, n) ->
f (car ls), if cdr ls then foldl (cdr ls), f, n else n
@brendanberg
brendanberg / coffee.html
Created August 24, 2011 20:28
CoffeeScript in Your Browser
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Caffeinate Me!</title>
<meta name="generator" content="TextMate http://macromates.com/">
<meta name="author" content="Brendan Berg">
<!-- Date: 2011-08-24 -->
<style>
@brendanberg
brendanberg / point.coffee
Created August 29, 2011 22:01
Making objects using closures in CoffeeScript
point = (x, y) ->
setX = (nx) -> x = nx
setY = (ny) -> y = ny
(n) -> n(x, y, setX, setY)
pointGetX = ->
(x, y) -> x
pointGetY = ->
(x, y) -> y
@brendanberg
brendanberg / random
Created October 27, 2011 15:34
Can you guess the output?
public static void main(String ... args) {
System.out.println(randomString(-229985452)+' '+randomString(-147909649));
}
public static String randomString(int seed) {
Random rand = new Random(seed);
StringBuilder sb = new StringBuilder();
for(int i=0;;i++) {
int n = rand.nextInt(27);
if (n == 0) break;
@brendanberg
brendanberg / gist:1556862
Created January 3, 2012 20:49
Evil dictionary insertions in Python
from time import time
def elapsed(fn):
t = time()
fn()
return time() - t
def insert_good(n):
for i in range(2 ** n):
d[i] = 0
@brendanberg
brendanberg / detector.py
Created March 10, 2012 00:06
Connect4 winner detection
import sys
import json
def search_line(row):
string = ''.join(row)
if string.find('OOOO') != -1:
return 'O'
if string.find('XXXX') != -1:
return 'X'
else:
@brendanberg
brendanberg / password.py
Created June 20, 2012 02:07
Generates every valid password for an incredibly stupid authentication system
import itertools
# Finding every single terrible password
#
# (Password requirements from http://kottke.org/12/06/the-worlds-worst-password-requirements-list)
#
# Must be exactly 8 characters long
# Must contain at least one character from each of these sets:
# ['@', '#', '$'],
# ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
@brendanberg
brendanberg / gist:4080423
Created November 15, 2012 18:49
A Parsing Expression Grammar to Turn Buffalo into Fish
/*
* Use this at http://pegjs.majda.cz/online
*
* Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo
* - becomes -
* Fishy fish fishy fish fish fish fishy fish
*
* Also, the grammar is recursive, so it could also generate ALL THE FISH.
*/