Skip to content

Instantly share code, notes, and snippets.

@shaunlebron
shaunlebron / hourglass.py
Last active September 29, 2015 03:37
Measuring arbitrary times using arbitrary hourglasses
def print_target_moves(aTimeCapacity, bTimeCapacity, targetTime):
'''
Given two hourglasses with respective time capacities,
print out a process for measuring the given target time
without going over that time.
An hour glass can only be flipped when any of them have run out.
TODO: generalize to multiple hour-glasses
'''
moves = []
@shaunlebron
shaunlebron / river.lp
Created March 27, 2012 23:31
Carcassonne Contiguous River Generator
% the tile width of the rectangular map
#const width=4.
positions(0..11).
rotations(0..3).
% piece(number, side, side type [g=grass, w=water, c=city, r=road]).
piece( 1,0,g ;; 1,1,g ;; 1,2,g ;; 1,3,w).
piece( 2,0,g ;; 2,1,g ;; 2,2,g ;; 2,3,w).
piece( 3,0,g ;; 3,1,w ;; 3,2,g ;; 3,3,w).
@shaunlebron
shaunlebron / README.md
Created May 31, 2012 15:00
Iterating Combinations

Iterating Combinations

If you're trying to iterate all combinations of k=5 on n=36, this is how you loop through them.

int k = 5;
int n = 36;
int count = 0;
for (int i1=1; i1 <= n-k+1; i1++)
@shaunlebron
shaunlebron / ConvertTableToTree.py
Created October 31, 2012 17:07
How readable is this?
entries = getListOfTableEntriesSomeWhere()
entryTree = MyTree(entries)
################################
def group_entries(groupclass, entries, keyfunc):
return [groupclass(k,g) for k,g in itertools.groupby(entries,keyfunc)]
key_sys = lambda x: x.sys
key_subsys = lambda x: x.subsys
@shaunlebron
shaunlebron / gfm-bug-example.md
Last active December 24, 2015 23:49
A GFM bug - displaying code after a list
  • item 1

  • item 2

  • item 3

    // code here

should be equivalent to...

  • item 1
@shaunlebron
shaunlebron / getSvgFrames.js
Created December 11, 2013 03:26
Convert SWF animation to SVG frames.
/*
You can use this script with Google Swiffy to produce SVG frames from an SWF animation.
1. Upload your SWF file to Google Swiffy --> https://www.google.com/doubleclick/studio/swiffy/
2. Save the swiffy output HTML page locally.
3. Edit the page to include this script with <script src="getSvgFrames.js"></script>
4. Open the page in Firefox to see the SVG download links appear for each frame.
*/
(function(){
var url = window.location.pathname;
@shaunlebron
shaunlebron / angleLerp.js
Created February 5, 2014 20:41
The best way to interpolate 2D angles
/*
2D Angle Interpolation (shortest distance)
Parameters:
a0 = start angle
a1 = end angle
t = interpolation factor (0.0=start, 1.0=end)
Benefits:
1. Angles do NOT need to be normalized.
// source: http://java.dzone.com/articles/using-lambda-expression-sort
// sort by first name
people.sort((p1, p2) -> p1.firstName.compareTo(p2.firstName));
// sort by full name
String fullName(Person p) { return p.firstName + " " + p.lastName; }
people.sort((p1, p2) -> fullName(p1).compareTo(fullName(p2)));
@shaunlebron
shaunlebron / om-string-cursor.cljs
Created June 26, 2014 20:05
problem with using strings as cursors in Om
; Suggested in Om Basic Tutorial for making strings work as cursors
(extend-type string
ICloneable
(-clone [s] (js/String. s)))
(extend-type js/String
ICloneable
(-clone [s] (js/String. s))
om/IValue
(-value [s] (str s)))
(def initial-app-state
{:page "search"
:market nil
:market-strategies []
:market-filter (default-filter)
:edit-strategy nil})