Skip to content

Instantly share code, notes, and snippets.

View samwho's full-sized avatar
🦀
Still in love with Rust.

Sam Rose samwho

🦀
Still in love with Rust.
View GitHub Profile
@samwho
samwho / VariableChangeSimulation.py
Created April 6, 2011 10:46
A simulation, in Python, of the Variable Change problem as explained in the movie "21".
import random
def get_choices():
'''Generates a list of size 3 that represents the doors. 0 is a loss (goat)
while 1 is a win (car). 1 door will have a 1 in it, the other two will have
0s.'''
choices = [0, 0, 0]
choices[random.randint(0, 2)] = 1
return choices
@samwho
samwho / HelloWorld.py
Created April 6, 2011 10:52
A very simple "Hello, world!" program in python.
print "Hello, world!"
@samwho
samwho / SimpleMaths.py
Created April 6, 2011 10:53
Some basic arithmetic in Python.
2 + 2 # addition, returns 4
4 - 1 # subtraction, returns 3
5 * 5 # multiplication, returns 25
2 ** 3 # 2 to the power of 3, returns 8
10 / 2 # division, returns 5
@samwho
samwho / FacebookUserAPICall.json
Created April 6, 2011 10:55
The JSON result from a simple Facebook user API request using the Graph API.
{"id":"544780370","name":"Sam Rose","first_name":"Sam","last_name":"Rose","username":"samwho","gender":"male","locale":"en_GB"}
@samwho
samwho / TheWhiteHouseBug.json
Created April 22, 2011 15:09
The singular tweet that represents the "whitehouse" bug in ThinkUp's Post API.
[
{
"id":15437714861,
"source":"twitter",
"location":"",
"place":null,
"geo":null,
"in_reply_to_user_id":null,
"is_reply_by_friend":false,
"in_reply_to_post_id":null,
@samwho
samwho / PHPNoticeBugCode.php
Created April 22, 2011 15:15
Lines 645, 646 and 647 of the PostAPIController class
if (isset($post->user->other->avg_tweets_per_day)) {
$post->user->avg_tweets_per_day = $post->user->other->avg_tweets_per_day;
}
@samwho
samwho / EmailPHPNoticeBug.txt
Created April 22, 2011 15:21
Email regarding PHP notice bug.
Okay, I've made significant progress on understanding the nature of this error.
It's, at its heart, very simple. $post->user->other is an array, not an object. Accessing it like an object was a very costly oversight on my part. It isn't tested for, either, because it was never a problem on my server and I'll explain why:
Take this script:
<?php
ini_set('display_errors', '1');
@samwho
samwho / fixture_generator.php
Created April 23, 2011 20:18
A script for turning an SQL query into a set of FixtureBuilder data for ThinkUp.
<?php
if (!isset($argv[1])) {
echo "You must specify an SQL query as the first parameter.\n";
exit;
} else {
if (strpos($argv[1], 'SELECT') !== 0) {
echo "Parameter must be a SELECT query. Please try again.\n";
exit;
}
$query = strval($argv[1]);
@samwho
samwho / TenLettersTenEnvelopes.rb
Created May 5, 2011 20:58
A simulation of the ten letters, ten envelopes problem.
# Original puzzle: A secretary receives 10 letters along with 10 addressed
# envelopes to be sent out. She accidentally drops the all the letters and
# envelopes and mixes them all up. So, she decides to put each of the 10
# letters randomly into one of the 10 addressed envelopes and mail them out.
# What is the probability that at least one letter arrives at the correct
# destination?
# Gets a hash that represents the envelopes containing their randomly placed
# letter. Both envelope and letter are represented by a number. If the key and
# the value of each item in the hash are the same, that is a correct
@samwho
samwho / RoutingUsage.php
Created May 18, 2011 01:21
Usage example of my PHP-Routing library.
<?php
// Include the Router class and a sample Controller to use.
require_once dirname(__FILE__) . '/lib/models/class.Router.php';
require_once dirname(__FILE__) . '/lib/controllers/class.FooController.php';
require_once dirname(__FILE__) . '/lib/controllers/class.NotFoundController.php';
// Get the singleton instance of the Router class.
$router = Router::getInstance();
/*