Skip to content

Instantly share code, notes, and snippets.

View lambda-fairy's full-sized avatar

Chris Wong lambda-fairy

View GitHub Profile
@lambda-fairy
lambda-fairy / ControlTest.hs
Created October 8, 2012 00:42
Thread spamming yay
import Data.Vector.Unboxed as V
main :: IO ()
main = print $ sumVector ones
sumVector :: (Num a, Unbox a) => Vector a -> a
sumVector = V.foldl' (+) 0
{-# NOINLINE ones #-}
ones :: Vector Int
@lambda-fairy
lambda-fairy / pqueue.js
Created December 6, 2012 02:51
Inefficient priority queue
/**
* Extremely inefficient priority queue.
*
* In a real program this would probably be implemented as a heap,
* but this is good enough for this demonstration.
*/
function PQueue(comparator) {
this.comparator = comparator || function(x, y) { return x - y }
this.array = []
}
@lambda-fairy
lambda-fairy / prims.js
Created December 6, 2012 04:38
Prim's algorithm, naive implementation
/**
* Prim's algorithm
*
* __(')< written by lambda fairy
* \___)
*/
function reset_graph(g) {
function reset(thing) {
thing.set_type(0)
@lambda-fairy
lambda-fairy / gist:4247514
Created December 9, 2012 23:38
Maze activity crash
Traceback (most recent call last):
File "/usr/bin/sugar-activity", line 146, in <module>
main()
File "/usr/bin/sugar-activity", line 104, in main
module = __import__(module_name)
File "/home/liveuser/Activities/Maze(GTK3).activity/activity.py", line 6, in <module>
from gi.repository import Gtk
File "/usr/lib64/python2.7/site-packages/gi/__init__.py", line 23, in <module>
from ._gi import _API, Repository
ImportError: could not import gobject (error was: ImportError('When using gi.repository you must not import static modules like "gobject". Please change all occurrences of "import gobject" to "from gi.repository import GObject".',))
@lambda-fairy
lambda-fairy / kol_turkey.py
Created December 14, 2012 22:36
Parse the KoL price log
"""Parse the file at <http://hogsofdestiny.com/kol/pricegun/dailyprice.log>"""
from urllib2 import urlopen
def read_prices(lines):
"""Given an iterator yielding the lines of the input file, return a dictionary mapping item IDs to prices."""
result = {}
for line in lines:
# Ignore comments
if line.startswith('#'):
@lambda-fairy
lambda-fairy / gist:4425147
Last active December 10, 2015 11:08
KoL tiny plastic results
You open the blind-pack capsules and dump out the toys inside:
You acquire an item: tiny plastic Charity the Zombie Hunter
You acquire 2 tiny plastic fire servants
You acquire an item: tiny plastic Scott the Miner
You acquire an item: tiny plastic beebee queue
You acquire an item: tiny plastic cavebugbear
You acquire 2 tiny plastic the Free Men
You acquire 2 tiny plastic Queen Bees
You acquire 2 tiny plastic angry space marines
You acquire an item: tiny plastic Norville Rogers
@lambda-fairy
lambda-fairy / kruskal.py
Created January 10, 2013 21:11
Kruskal's algorithm
"""Simple implementation of Kruskal's algorithm using a disjoint set"""
from collections import namedtuple
Edge = namedtuple('Edge', 'weight start end')
class DisjointSet:
def __init__(self, key):
@lambda-fairy
lambda-fairy / kitten.py
Created January 14, 2013 02:55
Monte Carlo simulation of button pushing game
#!/usr/bin/env python
from random import randint
def simulate():
"""Simulate one round of the game Gus described."""
pushes = 1
# Until a kitten pops out
while randint(0, 99) != 0:
# Push the button
@lambda-fairy
lambda-fairy / keks.py
Last active December 11, 2015 06:49
Keks solution
def max_index(items, start, end):
best_item = None
best_index = None
for index, item in zip(range(start, end), items[start:end]):
if best_item is None or item > best_item:
best_item = item
best_index = index
if best_index is None:
raise ValueError('maximum of empty sequence')
else:
@lambda-fairy
lambda-fairy / wormholes.cpp
Last active December 11, 2015 18:09
Wormholes solution
// Wormholes <http://nztrain.com/problems/98>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
// This can't be INT_MAX -- adding anything to INT_MAX would cause it to
// overflow and become negative.
// Kudos to Alan Ansell for pointing that out