Skip to content

Instantly share code, notes, and snippets.

@Radcliffe
Radcliffe / qwerty-to-dvorak.py
Last active February 28, 2019 14:38
Qwerty to Dvorak translator: Explaining XKCD 1787.
#!/usr/bin/env python
# This little Python script was written to explain XKCD comic #1787 (Voice Commands).
# In the comic, Cue Ball has reprogrammed his phone to use the Dvorak keyboard layout
# for voice recognition. (See http://xkcd.com/1787/)
# For those who don't know, the standard keyboard layout in the United States is called
# QWERTY, after the first six letters in the first row of letters. But there are a few
# enthusiasts who advocate an alternative layout, called Dvorak. These layouts have the
# same characters, but in different positions, as shown below.
@Radcliffe
Radcliffe / cryptarithm-solver.py
Created January 13, 2017 00:02
Python script to solve additive cryptarithms
#!/usr/bin/env python
# This programs solves additive cryptarithms using brute force.
# Example: solve_cryptarithm(['SEND', 'MORE', 'MONEY']) returns a list of
# all solutions to the equation SEND + MORE = MONEY, where each letter
# stands for a different digit in base 10. Leading zeros are not allowed.
from itertools import permutations
def isprime(n):
"""Check if a number is prime, using trial division"""
return n>1 and all(n % d for d in range(2, int(n**.5) + 1))
@Radcliffe
Radcliffe / 538.py
Last active October 17, 2016 23:29
Get most recent US presidential election predictions from FiveThirtyEight.com
import requests
from bs4 import BeautifulSoup
def main():
url = 'http://projects.fivethirtyeight.com/2016-election-forecast/mobile-bar.html'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'lxml')
return [(p.contents[0], p.contents[1].text)
for p in soup.find_all('p', class_ = 'value-wrap')]
@Radcliffe
Radcliffe / four-fours.py
Last active July 3, 2022 06:45
Python solver for puzzles like the "Four Fours" puzzle.
# This Python 3 script solves the "Four Fours" problem and similar problems.
# The example at the end of the script generates all combinations of [2,3,5,7]
# that equal 10, using the operations of addition, subtraction, multiplication,
# and division.
# Exact arithmetic is used. Only integer exponents between -99 and 99 are allowed.
from fractions import Fraction
@Radcliffe
Radcliffe / domino-tilings-by-height.csv
Created June 22, 2016 15:15
Number of domino tilings of an 8x8 chessboard, by height.
height n
0 1
1 1
2 2
3 5
4 10
5 18
6 32
7 59
8 102
@Radcliffe
Radcliffe / quicksort.js
Created June 15, 2016 14:07
Quicksort in JavaScript
function partition(arr, lo, hi) {
var swap = function (i, j) {
var tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
var pivot = arr[hi];
var pivotLoc = lo;
var i;
@Radcliffe
Radcliffe / list-of-fields-medalists.csv
Created May 13, 2016 19:10
List of Fields Medalists
YearAwarded Name Gender Citizenship SecondCitizenship BirthYear Remarks Affiliation AgeAtAward
2014 Artur Avila Cordeiro de Melo Male Brazil France 1979   CNRS - Institut de Mathématiques de Jussieu-Paris Rive Gauche 35
2014 Manjul Bhargava Male Canada United States 1974   Princeton University, 40
2014 Martin Hairer Male Austria   1975   University of Warwick 39
2014 Maryam Mirzakhani Female Iran   1977   Stanford University 37
2010 Ngô Bao Châu Male Vietnam France 1972   Université Paris-Sud 11 and Institute for Advanced Study 38
2010 Elon Lindenstrauss Male Israel   1970   Hebrew University of Jerusalem 40
2010 Stanislav K. Smirnov Male Russia   1970   University of Geneva 40
2010 Cédric Villani Male France   1973   École Normale Supérieure de Lyon and Institut Henri Poincaré 37
2006 Andrei Okounkov Male Russia   1969   Princeton University 37
@Radcliffe
Radcliffe / walmart-closings.csv
Created March 23, 2016 21:47
Walmart Store Closings Jan 2016
Location DateClosed StoreNumber StoreTypes lon lat
14331 Count Rd. 99, Headland, AL 2016-01-28 2173 Walmart Express -85.3259952 31.3607946
18 Apple Way, Ashford, AL 2016-01-28 2011 Walmart Express -85.237821 31.1765287
952 E. Lawrence Harris Hwy, Slocomb, AL 2016-01-28 2165 Walmart Express -85.5907905 31.1076635
407 West Washington St., Abbeville, AL 2016-01-28 2186 Walmart Express -85.2594391 31.5737215
6361 Hwy 72 East Gurley, AL 2016-01-28 2235 Walmart Express -86.3700933 34.6947341
87395 US Hwy 278, Snead, AL 2016-01-28 2260 Walmart Express -86.8494742 34.17204
3530 Cathedral Caverns Hwy, Grant, AL 2016-01-28 3769 Walmart Express -86.2611173 34.5014024
10188 Hwy 431 South, New Hope, AL 2016-01-28 3779 Walmart Express -86.3911528 34.5277698
720 N Hwy 71, Mansfield, AR 2016-01-28 2498 Walmart Express -94.2619279 35.077937
@Radcliffe
Radcliffe / cryptarithm-half-fifth-tenth-whole.py
Created March 9, 2016 23:34
Cryptarithm: HALF + FIFTH + TENTH + TENTH + TENTH = WHOLE
import itertools
for a,e,f,h,i,l,t,n,o,w in itertools.permutations(range(10)):
if h > 0 and t > 0 and f > 0 and w > 0:
half = 1000*h + 100*a + 10*l + f
fifth = 10100*f + i*1000 + t*10 + h
tenth = 10010*t + 1000*e + n*100 + h
whole = w*10000 + h*1000 + o*100 + l*10 + e
if half + fifth + 3*tenth == whole:
print "%s + %s + %s + %s + %s = %s" % (
half, fifth, tenth, tenth, tenth, whole)