Skip to content

Instantly share code, notes, and snippets.

View christianp's full-sized avatar

Christian Lawson-Perfect christianp

View GitHub Profile
from math import sqrt
from rtree import index
from itertools import product
from PIL import Image
from random import shuffle
import numpy
# granularity of colour comonents
component_max = 64
# values each colour component can take
@christianp
christianp / ness-upload-marks.user.js
Last active August 29, 2015 13:57
ness-upload-marks.user.js
// ==UserScript==
// @name NESS upload marks bongo
// @namespace http://www.staff.ncl.ac.uk/christian.perfect
// @version 0.1
// @description Set the fields in the NESS upload marks screen.
// @match https://ness.ncl.ac.uk/auth/marks/upload.php*
// @copyright 2014, Christian Perfect
// ==/UserScript==
document.querySelector('#ngl4').value=1;
Other nonpolar boathouses
by Unmellifluously Foreseeable
Nonmobility blasphemed borchers nonequivocally,
Amid onto refuelling tightroped spiers,
Punctured lutanist nonprudently overharshly twistingly nonpestilential some,
Rive soothing regorged notwithstanding itinerantly boundingly nonextricably mendeleev!
Foreseeing graveled swirl unchancy whichever enormously,
Fruitlessly along attrited influence regorged pettit broilingly autarchically jacketless.
Various fimbriated cabled beyond suboptically surreptitiously turpentine,
@christianp
christianp / changebase.bookmarklet.js
Last active August 29, 2015 13:58
Change the base of every number on the page
javascript:(function(){if(!window.currentBase) {window.currentBase = 10;}var digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';var subdigits = '\u2080\u2081\u2082\u2083\u2084\u2085\u2086\u2087\u2088\u2089';var base = undefined;while(isNaN(base)) {base = parseInt(prompt('What base would you like?'));}var sub_base = base==10 ? '' : (base+'').split('').map(function(d){return subdigits[d]}).join('');function parseBaseInt(s,base) {var n = 0;var p = 1;for(var i=s.length-1;i>=0;i--) {n += digits.indexOf(s.charAt(i))*p;p *= base;}return n;}function toBase(n,b) {if(n===0) {return '0';}var s = '';while(n) {var m = n%b;s = digits[m]+s;n = (n-m)/b;}return s;}function comma(n){var re = new RegExp('(['+digits+']{3}(?!.*\\.|$))','g');return n.toString().split('').reverse().join('').replace(re, '$1,').split('').reverse().join('');}function changeBase(s,n,commas,currentBase) {currentBase = parseInt(currentBase) || window.currentBase;if(commas) {n = n.replace(/,/g,'');n = toBase(parseBaseInt(n,window.cu
@christianp
christianp / gist:10852696
Last active August 29, 2015 13:59
Fix multiple spaces in em tags containing three or fewer words
html.css('em,strong').each do |e|
if e.content.split.length <= 3
e.search('//text()').each do |n|
if ends_with_space
n.content = n.text.gsub(/^\s/,"\u00A0")
end
n.content = n.text.gsub(/(?<=[\s\u00A0])\s/,"\u00A0")
ends_with_space = n.text.match(/\s$/)
end
end
@christianp
christianp / hangman.py
Created June 3, 2014 08:08
A hangman bot
import operator
import re
import random
words = [word.upper() for word in open('words.txt').read().split('\n')[:-1]]
class Hangman:
finished = False
def __init__(self,word):
@christianp
christianp / output.txt
Last active August 29, 2015 14:02
If sticker collectors pool their acquisitions, how much quicker do they finish their collections (and more cheaply)?
Album has 638 stickers
50 trials for each pool size
Collectors Waste Waste per collector
1 4054.360000 4054.360000
2 4748.840000 2374.420000
3 5328.000000 1776.000000
4 5586.160000 1396.540000
5 6429.320000 1285.864000
6 6675.840000 1112.640000
7 7084.840000 1012.120000
@christianp
christianp / output.txt
Created June 18, 2014 15:01
If sticker collectors pool their acquisitions but collectors leave the pool as soon as they're finished, how much does each collector spend?
Album has 638 stickers
50 trials for each pool size
Collectors Waste Waste per collector Mean Min bought Mean Max bought
1 3921.400000 3921.400000 4559.400000 4559.400000
2 4464.800000 2232.400000 2463.120000 3277.680000
3 5422.080000 1807.360000 2009.160000 2993.760000
4 5517.640000 1379.410000 1592.040000 2582.040000
5 6359.600000 1271.920000 1459.800000 2700.000000
6 6726.600000 1121.100000 1318.200000 2594.760000
7 7176.520000 1025.217143 1213.560000 2506.800000
@christianp
christianp / braille-puzzlebomb.py
Created November 5, 2014 09:17
Words whose Braille spellings overlap - inspired by Puzzlebomb September 2014 (http://aperiodical.com/2014/09/puzzlebomb-september-2014/)
from itertools import product,groupby
import re
import sys
alphabet = 'abcdefghijklmnopqrstuvwxyz'
#each letter is a grid of 6 dots
braille = ['100000','101000','110000','110100','100100','111000','111100','101100','011000','011100','100010','101010','110010','110110','100110','111010','111110','101110','011010','011110','100011','101011','011101','110011','110111','100111']
alpha_to_braille = {a:b for a,b in zip(alphabet,braille)}
@christianp
christianp / factorlines.py
Last active August 29, 2015 14:10
Solve the nrich factor lines puzzle - http://nrich.maths.org/1138
from itertools import product
# the size of the grid
columns = 5
rows = 5
# the cards we're given
cards = [1,2,3,21]
def valid_placement(card,place):