Skip to content

Instantly share code, notes, and snippets.

@shaunlebron
shaunlebron / hourglass.py
Last active Sep 29, 2015
Measuring arbitrary times using arbitrary hourglasses
View hourglass.py
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 Mar 27, 2012
Carcassonne Contiguous River Generator
View river.lp
% 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
Iterating Combinations
View README.md

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++)
View ConvertTableToTree.py
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 Dec 24, 2015
A GFM bug - displaying code after a list
View gfm-bug-example.md
  • item 1

  • item 2

  • item 3

    // code here

should be equivalent to...

  • item 1
@shaunlebron
shaunlebron / getSvgFrames.js
Created Dec 11, 2013
Convert SWF animation to SVG frames.
View getSvgFrames.js
/*
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 Feb 5, 2014
The best way to interpolate 2D angles
View angleLerp.js
/*
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.
View javaLambdaSort.java
// 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 Jun 26, 2014
problem with using strings as cursors in Om
View om-string-cursor.cljs
; 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)))
View app-state.clj
(def initial-app-state
{:page "search"
:market nil
:market-strategies []
:market-filter (default-filter)
:edit-strategy nil})