Skip to content

Instantly share code, notes, and snippets.

@Artanis
Artanis / axiom.py
Created August 2, 2012 18:43
Axiom of Choice?
import random
def choose(s, choice=None):
if choice is None:
choice = random.choice
if len(s) == 0:
raise KeyError("Cannot choose from empty set")
return choice(list(s))
@Artanis
Artanis / wbwb.py
Created January 18, 2012 07:06
Quick script tracking Wish Big Win Big tickets
# System Modules
from collections import OrderedDict, defaultdict
from itertools import chain
import json
# External Modules
# Local Modules
@Artanis
Artanis / example.py
Created January 10, 2012 23:42
str-in
def main():
while True:
user_input = raw_input("Enter 'q' to exit: ")
if user_input in ('q', 'Q'):
break
if __name__ == '__main__':
main()
@Artanis
Artanis / curses_bytesr.py
Created December 21, 2011 22:43
curses addstr bytestr
# System Modules
import curses
import time
# External Modules
# Local Modules
def test(stdscr):
@Artanis
Artanis / build.sh
Created September 17, 2011 08:43
Python C Extension Hello World
gcc -fpic --shared $(python-config --includes) greetmodule.c -o greetmodule.so
@Artanis
Artanis / fibonacci.js
Created September 8, 2011 00:08
Memoized, naïve, recursive Fibonacci function in JavaScript
// Take a function and return a function that caches the results of
// that function.
//
// Setting debug to true will print the logic the caching function uses.
var memoize = function(fn, debug) {
// Caching object, in the form {args : value}
var cache = {};
// Caching function. No explicit args. Any args are fed into the
// memoized function via apply().
@Artanis
Artanis / mtglife.py
Created July 5, 2011 00:31
Exceedingly simple life and poison counter tracker.
"""Simple class to track a player's life total and poison counts in
Magic: the Gathering.
Keeps a history of life total changes, and has some simple logic to
determine when a player has lost by on either life or poison counts--
not accounting for cards that alter the life and poison rules, however.
These 'deaths' are not enforced, so a player may be healed up to 1 or
more life, or have the tenth poison counter removed and the class will
happily report that the player is Still Alive.
@Artanis
Artanis / primes.py
Created June 18, 2011 02:48
Prime generator via unbounded Sieve of Erathosthenes
"""Prime generator via unbounded Sieve of Erathosthenes.
Ported from http://wthwdik.wordpress.com/2007/08/30/an-unbounded-sieve-of-eratosthenes/
For each prime number we store in a vector a pair consisting of
the prime itself and a multiple, initialized to the prime number
again. We compare each candidate with each multiple; if the
multiple is smaller we increment it by the corresponding prime.
Then if the candidate is equal to the multiple we discard it as
it’s obviously a multiple of the current prime, otherwise we
@Artanis
Artanis / ratelimit.py
Created May 14, 2011 07:24
Function decorator to limit calls to the function to a given rate.
"""Limit calls to a function to a given rate.
Intended for use with functions that interact with systems not under the
programmers control (for example, downloading html content from servers
for parsing).
usage:
# Two rate limited functions
@Artanis
Artanis / switch.py
Created April 8, 2011 10:53
A simple function (roughly) implementing C-style switch statements.
"""A simple function (roughly) implementing C-style switch statements.
Usage::
switch(value, {
'it':
"You said the word!",
'fruit':
"Suppose he's got a pointed stick.",