Skip to content

Instantly share code, notes, and snippets.

@brycethomas
brycethomas / monty_hall.py
Created November 22, 2014 07:10
Monty Hall
import random
cars, donkeys = 0, 0
trials = 10000
doors = ['a', 'b', 'c']
for i in range(trials):
# place the car behind a random door
car_door = random.choice(doors)
# choose first door at random
first_choice = random.choice(doors)
http://blog.sadphaeton.com/2009/01/20/python-development-windows-part-2-installing-easyinstallcould-be-easier.html
import httplib2
import StringIO
from PIL import Image
def get_tiles():
h = httplib2.Http('.cache')
base = 'http://www.jcu.edu.au/maps/townsville/interactive/mapimages/'
# get lvl 16 zoom tiles
for x in range(59482, 59486):
for y in range(36353,36357):
@brycethomas
brycethomas / gist:1153191
Created August 18, 2011 02:54
A 2-dimensional array of JButtons and setting text on them
// an array of JButtons
JButton[][] buttons = new JButton[4][4];
/* actually creates each of the sixteen JButton objects and sets
each button's text to look something like "Button [1][2]" (depending on row and column) */
for (int i=0; i < buttons.length; i++) {
for (int j=0; j < buttons[i].length; j++) {
buttons[i][j] = new JButton("Button [" + i + "][" + j + "]");
}
}
@brycethomas
brycethomas / gist:1153193
Created August 18, 2011 02:55
Creating a 2-dimensional array of JButtons and setting the text on them
// an array of JButtons
JButton[][] buttons = new JButton[4][4];
/* actually creates each of the sixteen JButton objects and sets
each button's text to look something like "Button [1][2]" (depending on row and column) */
for (int i=0; i < buttons.length; i++) {
for (int j=0; j < buttons[i].length; j++) {
buttons[i][j] = new JButton("Button [" + i + "][" + j + "]");
}
}
@brycethomas
brycethomas / gist:1154251
Created August 18, 2011 15:07
Apples simulator - amended main method and toString() method
// prints out something like "Apple 27 (GRANNY_SMITH, 3)"
public String toString() {
return String.format("Apple %d (%s, %s)",size,type,batchNumber);
}
public static void main(String[] args) {
Random random = new Random(); // this is the line missing from the workshop pdf
final int SIZE = random.nextInt(5) + 1;
Apple[] apples = new Apple[SIZE]; // a randomly sized apples array
for (int i = 0; i < SIZE; ++i) {
@brycethomas
brycethomas / function_pointer_delegates.py
Created June 16, 2012 16:16
Function pointers up to delegates
# some button class somewhere that you didn't write.
class Button():
def __init__(self,functionToExecuteOnClick):
self.clickFunction = functionToExecuteOnClick
# does a bunch of other cool stuff to display button - you were too slack to write this code, that's why you're using their button in the first place.
# pretend this is private. other classes shouldn't be calling this method
@brycethomas
brycethomas / enter-caps
Created October 15, 2017 02:20
Run a command with ambient capabilities
#!/bin/bash
# Run from terminal like enter-caps ./wireshark
export PS1="#$PS1"
export WIRESHARK_RUN_FROM_BUILD_DIRECTORY=1
caps=cap_net_admin,cap_net_raw
sudo -E capsh --keep=1 --caps="cap_setuid,cap_setgid,cap_setpcap+ep $caps+eip" --user=[my_hardcoded_username] --addamb="$caps" -- -c "$@"
echo "...Exiting enter-caps"