Skip to content

Instantly share code, notes, and snippets.

@Radcliffe
Radcliffe / check_sudoku.py
Last active December 26, 2015 18:09
Python code to test if the input is a valid Sudoku puzzle
from collections import Counter
def sanity_check(grid):
if not(isinstance(grid,list)
and len(grid) == 9
and all(isinstance(row, list)
and len(row) == 9
for row in grid)
and all(isinstance(elt, int)
and 0 <= elt <= 9
@Radcliffe
Radcliffe / fuzzer.py
Created October 29, 2013 02:45
A fuzzer. The script attempts to crash Foxit Reader and Adobe Reader by opening corrupted PDF files. I would not run this script if I were you.
#!/usr/bin/python
# 5-line fuzzer below is from Charlie Miller's
# "Babysitting an Army of Monkeys"
from os import listdir
from os.path import isfile, join
mypath = '/users/david/documents/python/pdf/'
file_list = [ mypath+f for f in listdir(mypath) if isfile(join(mypath,f)) ]
@Radcliffe
Radcliffe / equal_powers.py
Last active December 29, 2015 22:59
Python 2.7 program to check whether two integer powers are equal, WITHOUT computing the powers.
# Python 2.7 program to check whether two integer powers are equal,
# WITHOUT computing the powers.
#
# Author: David Radcliffe, 1 December 2013.
ALLOW_ZERO_TO_ZERO = True
# If True, assume that 0 to the 0 is 1.
# If False, assume that 0 to the 0 is undefined.
# Return the sign of a^b. There are four possible results
@Radcliffe
Radcliffe / gist:7906863
Last active December 31, 2015 00:19
Solution to a problem posed by Deomani Sharma Ramsurrun
# Solution to a problem posted by Deomani Sharma Ramsurrun
# David Radcliffe 11 Dec 2013
# This code is in the public domain
###########################################################
import fractions
# Generate all non-negative integer solutions to the system
# ab + ac + ad = 6
# ab + bc + bd = 9
@Radcliffe
Radcliffe / fivecircles.py
Last active December 31, 2015 09:49
Given a square 5x5 grid of points, find five circles that together pass through all 25 points.
#!/usr/bin/env/python
# Author: David Radcliffe (dave@gotmath.com)
# Date: 20 December 2013
# This code is in the public domain.
# Problem: Given a square 5x5 grid of points, find five circles that together pass through all 25 points.
# There are 21 essentially different solutions. Two solutions are considered the same if they differ only
# by a rotation or reflection. This script finds all of these solutions.
# The solutions are rendered graphically at http://www.gotmath.com/doc/fivecircles.html
@Radcliffe
Radcliffe / _.md
Created December 19, 2013 08:00
Covering a grid with circles
@Radcliffe
Radcliffe / _.md
Created December 21, 2013 16:04
Covering a grid with circles
@Radcliffe
Radcliffe / _.md
Created February 15, 2014 20:31
Tributary inlet
#!/usr/bin/env python
#
# This Python 2.7 script solves the following probability problem:
# If 20 distinct positive integers are chosen from the range 1 - 80
# (without replacement) what is the probability that their sum is 810?
# Source: Kim Zafra, http://lnkd.in/bmg4AR5
# Remarks:
@Radcliffe
Radcliffe / shufflesquare.py
Created April 21, 2014 13:43
Numbers whose squares can be rearranged to form exactly two other squares
#!/usr/bin/env python
#
# List the numbers n such that the digits of the square of n
# can be rearranged to form exactly two other squares.
# For example, 13 belongs to list because 13*13 = 169, and
# the digits of 169 can be rearranged to form the squares 196
# (= 14*14) and 961 (= 31*31); but no other squares can be formed
# by rearranging the digits of 169.
#
# Inspired by a question of James Tanton (@jamestanton on Twitter).