Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env python
# Simulation of the 'Can you cross the river' riddler problem:
# https://fivethirtyeight.com/features/night-falls-a-storm-rolls-in-can-you-cross-the-river/
#
# n
# / | \
# a - b - c
# | | |
# d - e - f
# \ | /
@yourcelf
yourcelf / 2016.py
Last active January 7, 2016 22:14
Find all ways to create "2016" using the numbers 1,2,3,4,5 and basic operations (parentheses, multiplication, addition, subtraction, exponentiation).
from __future__ import division, print_function
from itertools import permutations, combinations_with_replacement, product
def guarded_exponentiation(a,b):
"""
Raise a to the power of b, but only if the result won't be silly large, as
silly largeness slows us way down.
"""
if (a > 1 or a < -1) and b > 1000:
raise OverflowError
### Keybase proof
I hereby claim:
* I am yourcelf on github.
* I am cfd (https://keybase.io/cfd) on keybase.
* I have a public key whose fingerprint is A1F8 409D BC1A 0A62 AD20 EC7F 2BAC A497 F566 4872
To claim this, I am signing this object:
@yourcelf
yourcelf / gist:084e8a0d73ef8f9a6809
Created September 12, 2014 21:31
velocity / npm / path issue
=> Started proxy.
=> Meteor 0.9.1.1 is available. Update this project with 'meteor update'.
=> Started MongoDB.
npm: updating npm dependencies -- temp, path, request, twilio, googleapis...
I20140912-15:30:22.408(-6)? [velocity] PWD /home/dev/git/Admit
I20140912-15:30:22.518(-6)? Check for test package configs... [ 'packages/HTML5-History-API/smart.json',
I20140912-15:30:22.518(-6)? 'packages/autoform/smart.json',
I20140912-15:30:22.519(-6)? 'packages/blaze-layout/smart.json',
I20140912-15:30:22.519(-6)? 'packages/bootstrap-3/smart.json',
I20140912-15:30:22.527(-6)? 'packages/bootstrap3-wysihtml5/smart.json',
I20140910-08:17:22.677(-6)? [velocity] retrying mirror at http://localhost:5000/ connect ECONNREFUSED
npm: updating npm dependencies -- temp, path, request, twilio, googleapis...
I20140910-08:17:37.236(-6)? Jasmine-Unit is loaded
I20140910-08:17:37.463(-6)? [PackageStubber] custom stub found for core package jquery
I20140910-08:17:37.463(-6)? [PackageStubber] custom stub found for core package underscore
I20140910-08:17:37.465(-6)? [PackageStubber] community stub found for iron-core
I20140910-08:17:37.466(-6)? [PackageStubber] community stub found for iron-router
I20140910-08:17:37.466(-6)? [PackageStubber] community stub found for router
I20140910-08:17:37.466(-6)? [PackageStubber] won't auto-stub these packages: [ 'jasmine-unit',
I20140910-08:17:37.466(-6)? 'jasmine',
@yourcelf
yourcelf / tex-conversions.py
Created July 22, 2013 17:39
Convert utf-8 encoded unicode characters to latex escape sequences. I use this for pre-processing bibtex exports from zotero. Usage: tex-conversions.py infile > outfile
# Obtained from http://code.activestate.com/recipes/252124-latex-codec/
latex_equivalents = {
0x0009: ' ',
0x000a: '\n',
0x0023: '{\#}',
0x0026: '{\&}',
0x00a0: '{~}',
0x00a1: '{!`}',
0x00a2: '{\\not{c}}',
0x00a3: '{\\pounds}',
@yourcelf
yourcelf / gist:5819531
Created June 20, 2013 01:12
find_palindromes.py
# Find unintentional palindromes in a text file.
import re
import argparse
from collections import defaultdict
parser = argparse.ArgumentParser(description="Find unintentional palindromes in a text file.")
parser.add_argument("filename", help="The name of the file to look in.")
parser.add_argument("--min", help="Minimum length", default=4)
parser.add_argument("--max", help="Maximum length", default=500)
@yourcelf
yourcelf / servethis
Created March 7, 2013 00:36
Run python's simple http server in the current directory with the specified port.
#!/usr/bin/env python
import sys
import subprocess
ports = sys.argv[1:] or [8000]
procs = []
for port in ports:
procs.append(
@yourcelf
yourcelf / soma.py
Last active June 10, 2019 14:32
Command line script to play somafm radio stations. Type `soma.py --help` for usage, but briefly: `soma.py <stationname>` plays the station, `soma.py` with no argument lists the available stations. Uses mplayer to do the playing (change with --player option).
#!/usr/bin/env python
from __future__ import print_function
import sys
import subprocess
import argparse
import textwrap
import signal
stations = {
@yourcelf
yourcelf / check_inclusion.py
Created February 17, 2013 16:05
Python script to check whether all the files found anywhere in the source directory are also found somewhere in the dest directory. I use it for, e.g., cleaning up after importing mp3's or photos using a program that changes directory layouts. Checks identity using either md5sum or filename. Run with "--help" for usage.
#!/usr/bin/env python3.2
import os
import sys
import hashlib
import argparse
from functools import partial
def md5sum(filename):
# http://stackoverflow.com/a/7829658