Skip to content

Instantly share code, notes, and snippets.

# Dynamic programming solution to the ugly numbers problem.
# Author: Jon Brandvein
# See problem at: https://www.codeeval.com/public_sc/42/
from itertools import chain
from functools import lru_cache
uglyprimes = [2, 3, 5, 7]
def ugly(n):
@brandjon
brandjon / pilwrapper.py
Created February 3, 2015 21:41
A wrapper around PIL (Pillow) for easy loading/saving of images and accessing pixel contents as a 2d array. Pretty much superseded by the image load() method, which gives direct pixel access.
"""
pilwrapper.py: A simple wrapper around the Python Imaging Library,
or its replacement, Pillow.
Copyright (c) 2012-2014 Jon Brandvein
Freely available under the MIT License (http://opensource.org/licenses/MIT).
"""
__version__ = '0.1.0'
@brandjon
brandjon / md2rst_striplinks.py
Last active August 29, 2015 14:11
Convert markdown to rst and remove links along the way
@brandjon
brandjon / toadsfrogs.py
Created June 2, 2013 18:33
Python implementation of Toads and Frogs solver.
# Python implementation of Toads and Frogs.
# See http://en.wikipedia.org/wiki/Toads_and_Frogs_(game)
# Author: Jon Brandvein
# Toads move right, frogs move left. Each piece can jump over
# the opposite kind of piece.
p1_rules = [
('t.', '.t'),
('tf.', '.ft'),
@brandjon
brandjon / toadsfrogs.P
Last active December 16, 2015 14:19
Back-tracking solver of Toads and Frogs game.
% Program to find all winning plays of Toads and Frogs.
% See http://en.wikipedia.org/wiki/Toads_and_Frogs_(game)
% Author: Jon Brandvein
% Changes:
% 05/02/13 - eliminate list processing helper rules in favor of
% a simpler append-based definition of player moves
% (thanks to David Warren)
:- import select/3, append/3 from basics.