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 / SUPER_AWESOME_ENCODER_THINGY.py
Last active December 19, 2015 04:39
SUPER AWESOME ENCODER THINGY
"""
========================================================================
SUPER AWESOME ENCODER THINGY
------------------------------------------------------------------------
BY LAMBDA FAIRY COPYRIGHT BLAH BLAH BLAH DON'T COPY OR YOU WILL
>>>>>>>> DIIIIIIIIIIIIIIIIIIIIEEEEEEEEEEEEEEEEEEEE <<<<<<<<
========================================================================
"""
@lambda-fairy
lambda-fairy / NotStatistics.hs
Created May 22, 2013 00:45
Definitely not statistics
import qualified Data.Sequence as S
import Data.Foldable (toList)
movingMean :: Int -> [Double]
slices :: Int -> [a] -> [[a]]
slices k ys = case takeMaybe k xs of
Nothing -> []
Just start -> start : loop (S.fromList start) ys
where
@lambda-fairy
lambda-fairy / gist:5471926
Created April 27, 2013 04:56
Fenwick trees
#!/usr/bin/env python3
class Fenwick:
"""A specialized data structure for representing range sums."""
def __init__(self, size, identity=0):
self.tree = [identity] * (size + 1)
self.identity = identity
@lambda-fairy
lambda-fairy / read.py
Last active December 14, 2015 07:48 — forked from anonymous/read.py
Simple input loop demonstrating higher-order functions
"""
Simple input loop thing, with ducks.
By Lambda Fairy (github.com/lfairy)
Examples::
n = read(int, 'Enter a number: ')
MIN = 1
"""
=============================
The Epic Fail Guessing Game
=============================
The Epic Fail Guessing Game (*efguess*) is a simple number guessing game.
Except you can't win it, of course. That would be silly.
"""
@lambda-fairy
lambda-fairy / eko.cpp
Created January 31, 2013 00:28
Eko solution
// Eko <http://nztrain.com/problems/56>
#include <cstdio>
#include <climits>
using namespace std;
long long total_wood(int *trees, size_t n_trees, int sawblade) {
long long total = 0;
for (size_t i = 0; i != n_trees; ++i) {
if (trees[i] > sawblade) {
@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
@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 / 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 / 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):