Skip to content

Instantly share code, notes, and snippets.

🎶 around the world 🕺🎶

Philipe Fatio fphilipe

🎶 around the world 🕺🎶
View GitHub Profile
View output.txt
Calculating -------------------------------------
on_server_error :notify_honeybadger
90.832k i/100ms
on_server_error &Honeybadger.method(:notify)
96.540k i/100ms
on_server_error { |e| Honeybadger.notify(e) }
90.455k i/100ms
on_server_error :notify_honeybadger
2.007M (± 7.9%) i/s - 9.992M
fphilipe /
Last active Dec 20, 2015
Monte Carlo Sampling of π

From the Wikipedia article on Monte Carlo method:

Monte Carlo methods (or Monte Carlo experiments) are a broad class of computational algorithms that rely on repeated random sampling to obtain numerical results; [...]

Given that the circle and the square have a ratio of areas that is π/4, the value of π can be approximated using a Monte Carlo method:

  1. Draw a square on the ground, then inscribe a circle within it.
  2. Uniformly scatter some objects of uniform size (grains of rice or sand) over the square.
  3. Count the number of objects inside the circle and the total number of objects.
  4. The ratio of the two counts is an estimate of the ratio of the two areas, which is π/4. Multiply the result by 4 to estimate π.
fphilipe / main.m
Created Aug 21, 2012
Objective-C -hash method clash
View main.m
// This code demonstrates -hash collision on NSString. Two strings have the same
// hash when the first, center, and last 32 chars are identical. The other chars
// don't matter at all.
// I had to find this out the hard way when loading images from cache while
// using the url string hash as the cache key. Unfortunately the urls were all
// gravatar urls, thus first 32 chars were identical. Further, all urls also had
// a fallback image url appended as query which covered the 32
// center and end chars. The varying part was between the start and center parts
// and thus the hash was identical for all urls.
View _setup.sql
-- Create users table with username as PK.
CREATE TABLE users_no_id (username varchar primary key, age int);
-- Create posts table with reference to users table using its PK and index on the reference.
CREATE TABLE posts_no_id (id serial primary key, username varchar references users_no_id (username), body text);
CREATE INDEX ON posts_no_id (username);
-- Create 1000000 users:
INSERT INTO users_no_id (username, age) SELECT substring(md5(random()::text) || md5(random()::text), (random()*32)::int, 16 + (random()*16)::int), 18 + (random()*70)::int FROM generate_series(1, 1000000);
-- Create 200000 posts for random users (multiple queries so users can have mutliple posts).
INSERT INTO posts_no_id (username, body) SELECT username, md5(random()::text) || md5(random()::text) FROM users_no_id ORDER BY random() LIMIT 25000;
INSERT INTO posts_no_id (username, body) SELECT username, md5(random()::text) || md5(random()::text) FROM users_no_id ORDER BY random() LIMIT 25000;
View gist:4058176
module Base62
BASE_CHARS = '0SKVnQFHhGs9qo7p8cRW54duMYNXTCErx2tDmwO3kabfUB1elLvjiIgyJAZ6Pz'.split('')
def self.decode(number)
# assure string
number = number.to_s.reverse.split('')
decoded = 0
View obike-zurich.geojson
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
fphilipe /
Last active May 7, 2019
Badiwassertemperaturen und -öffnungszeiten

Badiwassertemperaturen und -öffnungszeiten

A simple one-liner that results in this:

|  Badi                         | Wassertemp. | Öffnungszeit            |
|  Flussbad Au-Höngg            | 17          | geschlossen             |
|  Flussbad Oberer Letten       | 16          | geschlossen             |
fphilipe / exclude.sql
Last active Oct 13, 2021
PostgreSQL EXCLUDE constraint
View exclude.sql
CREATE TABLE room_reservations (
room_id integer,
reserved_at timestamptz,
reserved_until timestamptz,
canceled boolean DEFAULT false,
room_id WITH =, tstzrange(reserved_at, reserved_until) WITH &&
) WHERE (not canceled)